As followed my post about simple java TCP server and I promised to post about simple java TCP client, so here it is. If you haven't read my simple java TCP server already, then why don't you give it a go. There you'll find how to compile java code that I will not be covering in this post. So go ahead to this post and get back here as soon as you know how to compile java code. If you already know how to do it, then it's okay to continue to read this post.
To test this out, we need to run the server as well so go ahead open another terminal and run the server. Put the socket port you want your server will be using and press ENTER . Then in your client, put the server port that you just typed in and press ENTER. Now you'll see "Ready to send message" message, it indicates that both client and server are now connected.
Just type something from client then you'll see your message will be appeared in the server side.
To test our server from a different computer, we need know our PC ip address. To find out our IP address, simply type in terminal ipconfig for windows or ifconfig for ubuntu and our IP address is shown next to inet label.
After we know our PC's IP, open terminal from another computer which runs ubuntu and type in telnet <our ip address> <server port> and we're ready to send message to server.
#source: https://systembash.com/a-simple-java-tcp-server-and-tcp-client/
import java.io.*; import java.net.*; import java.util.Scanner; class Client{ private static Client client; private static int socketNumber = 1234; private static Socket clientSocket; public Client(){ try{ clientSocket = new Socket("localhost", getSocket()); }catch(Exception e){ System.out.println("Error."+e.getMessage()); } } private int getSocket(){ Scanner in = new Scanner(System.in); System.out.print("Input socket server: "); socketNumber = in.nextInt(); System.out.println("Socket server "+socketNumber+", Ready to send message"); return socketNumber; } public static void main(String argv[]) throws Exception{ client = new Client(); String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }Compile and run the code.
To test this out, we need to run the server as well so go ahead open another terminal and run the server. Put the socket port you want your server will be using and press ENTER . Then in your client, put the server port that you just typed in and press ENTER. Now you'll see "Ready to send message" message, it indicates that both client and server are now connected.
Just type something from client then you'll see your message will be appeared in the server side.
To test our server from a different computer, we need know our PC ip address. To find out our IP address, simply type in terminal ipconfig for windows or ifconfig for ubuntu and our IP address is shown next to inet label.
After we know our PC's IP, open terminal from another computer which runs ubuntu and type in telnet <our ip address> <server port> and we're ready to send message to server.
#source: https://systembash.com/a-simple-java-tcp-server-and-tcp-client/
Comments
Post a Comment