Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.Scanner;
- public class TCP_Client
- {
- public static void main(String[] args) throws IOException
- {
- int port = 6666;
- String ip ="127.0.0.1";
- String msg;
- Scanner scan =new Scanner(System.in);
- try {
- // Connect with server
- Socket connection = new Socket(ip, port);
- System.out.println("Connected! " + connection);
- // Setting up input and output streams
- DataOutputStream outputStr = new DataOutputStream(
- connection.getOutputStream());
- DataInputStream inputStr = new DataInputStream(
- new BufferedInputStream(connection.getInputStream()));
- // Start communication
- msg = scan.next();
- outputStr.writeUTF(msg);
- outputStr.flush();
- String message = inputStr.readUTF();
- System.out.println("TCP_Server> " + message);
- // Close streams and socket
- outputStr.close();
- inputStr.close();
- connection.close();
- } catch (IOException ex) {
- System.out.println("Connection error! " + ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement