Created
September 6, 2019 00:42
-
-
Save muddokon/74b21843ca12c8eb74ae6f36721aadf7 to your computer and use it in GitHub Desktop.
TCPClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
public class TCPClient { | |
public static void main (String args[]) { | |
// arguments supply message and hostname | |
Socket s = null; | |
try{ | |
int serverPort = 7896; | |
s = new Socket(args[1], serverPort); | |
DataInputStream in = new DataInputStream( s.getInputStream()); | |
DataOutputStream out =new DataOutputStream( s.getOutputStream()); | |
out.writeUTF(args[0]); // UTF is a string encoding see Sn. 4.4 | |
String data = in.readUTF(); // read a line of data from the stream | |
System.out.println("Received: "+ data) ; | |
}catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage()); | |
}catch (EOFException e){System.out.println("EOF:"+e.getMessage()); | |
}catch (IOException e){System.out.println("readline:"+e.getMessage()); | |
}finally {if(s!=null) try {s.close();}catch (IOException e){System.out.println("close:"+e.getMessage());}} | |
} | |
} | |
//Code by Coulouris et al |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment