Socket chat with socket in Java
1, what is a socket
Socket refers to a network socket, what is a socket? This is a network of end-to-end network protocol, the port is the process number, the socket in the network so that the two-terminal communication to form a port to communicate directly, so the parameters of the socket is the two ends of the IP and port number;
Again in the network, there must be someone to pay, in the network of other people link, otherwise you want to connect others, others at two point want to even you, how can not even on, always have yo individual need to do the role of waiting, this role is the service end is ServerSocket, He provides a fixed IP and port number socket in the network;
2, implementing the socket interface in Java
In Java, two are provided to establish a socket link in the java.net package, the client to use the socket (addr,port)//parameter is the address and port number of the remote service, and the other on the server with ServerSocket (port) Create a waiting socket and use serversocket.accept () to dynamically wait on the server and accept a socket request to request a link
Once the Serversocket.accept () method obtains a link, it encapsulates a socket object that can be used by the server to obtain the client's information and send the message to the client; say, on the code;
PackageTestpackage;/** @author: Mayeye * @about: Service side **/Importjava.net.*;ImportJava.io.*; Public classTestServer { Public Static voidMain (string[] args) {//try {System.out.println ("============================="); ServerSocket Server=NULL; Try{Server=NewServerSocket (1233); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Socket Client=NULL; Try{Client=server.accept (); System.out.println ("IP is:" +client.getinetaddress () + "Client link"); System.out.println ("Port is:" +client.getport () + "Client link"); System.out.println ("LocalPort for:" +client.getlocalport () + "Client link"); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } String Line=NULL; BufferedReader is=NULL; Try{ is=NewBufferedReader (NewInputStreamReader (Client.getinputstream ())); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } printwriter os=NULL; Try{OS=NewPrintWriter (Client.getoutputstream ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } BufferedReader sin=NewBufferedReader (NewInputStreamReader (system.in)); Try{System.out.println ("Client:" +is.readline ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{ Line=Sin.readline (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } while(!line.equals ("Bye") {os.println (line); Os.flush (); System.out.println ("Server:" +Line ); Try{System.out.println ("Client:" +is.readline ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{ Line=Sin.readline (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} os.close (); Try{is.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{client.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{server.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("********************************"); /*}catch (Exception e) {System.out.println ("-------------------------------"); }*/ }}
Above is the service-side code
PackageTestpackage;Importjava.net.*;ImportJava.io.*; Public classTestsocket { Public Static voidMain (string[] args) {Try{Socket Client=NewSocket ("127.0.0.1", 1233); BufferedReader Sin=NewBufferedReader (NewInputStreamReader (system.in));//building a standard input streamPrintWriter os=NewPrintWriter (Client.getoutputstream ()); BufferedReader is=NewBufferedReader (NewInputStreamReader (Client.getinputstream ()));//constructs an input stream from a client objectString ReadLine; System.out.println ("IP is:" +client.getinetaddress () + "Client link"); ReadLine=Sin.readline (); while(!readline.equals ("Bye") {os.println (readline); Os.flush (); System.out.println ("Client:" +ReadLine); System.out.println ("---*****************---"); System.out.println ("Server:" +is.readline ()); ReadLine=sin.readline ();//reads a string s from the system standard input} os.close (); Is.close (); Client.close (); } Catch(unknownhostexception e) {//TODO auto-generated Catch blockSystem.out.print ("==================== Cannot find host name"); } Catch(IOException e) {System.out.print ("===================io Error"); } }}
The above is the service-side code;
Everyone is an adult, so the directory structure doesn't need me to say it.
This is just a Java implementation of the most simple socket chat, you must ask a reply, and no interface;
Preview, next blog I will be introduced to the interface of the solution;
Talk about sockets and Java