Java Socket Emulation Chat Program

Source: Internet
Author: User



According to TCP/IP protocol, the network chat via socket is divided into server side and client, which is the reference program.

Server-side programs:

Package Com.michael.contact;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import java.io.outputstream;import java.net.serversocket;import java.net.Socket; public class Server {public static void main (string args[]) throws IOException {string reces = Null;int len; Socket socket = Null;outputstream OutputStream = Null;inputstream InputStream = null;byte[] Rece = new Byte[1000];try {Ser Versocket ServerSocket = new ServerSocket (8888); The socket for the server, Port 8888while (true) {socket = serversocket.accept (); inputstream = Socket.getinputstream ();// Get input Stream outputstream = Socket.getoutputstream ();//Get output stream len = Inputstream.read (rece);//Accept Client message if (len! = 0) reces = new Strin G (rece, 0, Len); System.out.println (reces); BufferedReader bufferreader = new BufferedReader (new InputStreamReader (system.in)); Outputstream.write ("Server ...." + Bufferreader.readline ()). GetBytes ());//Return to the client Welcome message}} catch (Exception e) {e.printstacktrace ();} finally {inputsTream.close (); Outputstream.close (); Socket.close ();//Remember to close these inputs, output streams, and sockets}}} 

Client program:

Package Com.michael.contact;import Java.io.BufferedReader;  Import java.io.IOException;  Import Java.io.InputStream;  Import Java.io.InputStreamReader;  Import Java.io.OutputStream;  Import java.net.InetAddress;  Import Java.net.Socket; public class Client {public static void main (string[] args) throws IOException {String serveripaddress = "          127.0.0.1 ";//each other's IP string words =" Hello ";//The other's words string temp = null;          byte[] Serversay = new byte[1000];//read <1kb inputstreamreader read = null;          int len = 0;          Socket s = null;          OutputStream outputstream = null;          InputStream inputstream = null;          Get the IP System.out.println you gave him ("Please enter each other's IP (default to yourself):");          Read = new InputStreamReader (system.in);              try {temp = new BufferedReader (read). ReadLine ();          if (!temp.equals ("")) serveripaddress = temp;        } catch (IOException E1) {}  Get you each of his message System.out.println ("Please enter what you want to say to him (her)----Enter quit!!:");          Read = new InputStreamReader (system.in);                  while (true) {try {temp = "Hello";                  temp = new BufferedReader (read). ReadLine ();                  words = temp;                  if (Words.equals ("Quit")) break;                  temp = "Client ..." + words; Initiating communication s = new socket (Inetaddress.getbyname (serveripaddress), 8888);//Socket IP address and port number out                  Putstream = S.getoutputstream ();                  InputStream = S.getinputstream ();                  Outputstream.write (Temp.getbytes ("GBK"));//Send message to Server Len = Inputstream.read (Serversay);//Accept Server message              System.out.println (New String (Serversay, 0, Len));//Client console displays the information returned by the server} catch (Exception e) {                  } finally {inputstream.close ();      Outputstream.close ();            S.close ();//Remember to close these inputs, output streams and sockets}}//end While}}   


Java Socket Emulation Chat Program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.