Communication between the Socket server and client in JAVA
Socket server/Client Communication
(Simple)
Server: receives messages from the client. Client: receives keyboard input and sends messages to the server.
Simple functions. Other functions share the same principle, but they only provide reference for the small code framework.
Client:
Package com. cr. bean; import java. io. bufferedReader; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. inputStreamReader; import java.net. inetAddress; import java.net. inetSocketAddress; import java.net. socket; import java. util. extends; public class client001 {@ SuppressWarnings ("resource") public static void main (String [] args) {extends = new builds (System. in); Socket mysocket = null; DataInputStream in = null; DataOutputStream out = null; try {mysocket = new Socket (); System. out. println ("Enter the Server IP:"); String IP = bytes. nextLine (); System. out. println ("enter the port number:"); int port = bytes. nextInt (); if (mysocket. isConnected () {} else {// connect to the server, using multithreading technology InetAddress = InetAddress. getByName (IP); InetSocketAddress socketAddress = new InetSocketAddress (address, port); mysocket. connect (socketAddress); System. out. println (socketAddress. toString (); in = new DataInputStream (mysocket. getInputStream (); out = new DataOutputStream (mysocket. getOutputStream ();} catch (Exception e) {System. out. print ("server connection disconnected" + e);} while (true) {String str = null; try {str = failed. nextLine (); out. writeUTF (str);} catch (Exception e) {System. exit (0 );}}}}
Server:
Package com. cr. bean; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java.net. inetAddress; import java.net. inetSocketAddress; import java.net. serverSocket; import java.net. socket; public class serve001 {@ SuppressWarnings ("resource") public static void main (String [] args) {ServerSocket server = null; Socket you = null; while (true) {try {server = new ServerSocke T (7777);} catch (IOException e) {System. out. println ("listening ...... ");} Try {you = server. accept (); System. out. println ("customer address:" + you. getInetAddress ();} catch (IOException e) {System. out. println ("waiting for customers");} if (you! = Null) {new ServerThread (you ). start (); // start a special Thread for each customer }}} class ServerThread extends Thread {Socket socket; DataOutputStream out = null; DataInputStream in = null; string s = null; ServerThread (Socket t) {socket = t; try {out = new DataOutputStream (socket. getOutputStream (); in = new DataInputStream (socket. getInputStream ();} catch (Exception e) {e. printStackTrace () ;}@ Overridepublic void run () {try {while (true) {String str = in. readUTF (); System. out. println ("the client sends the message to the server:" + str) ;}} catch (Exception e) {System. out. println ("customer leaves ");}}}
Run the test:
Relatively basic code is not explained