Socket usage, simple simulation of one-to-one chat

Source: Internet
Author: User

Http://blog.csdn.net/touch_2011/article/details/6914739

1. The communication between socket and serversocket is described as follows. Note how the client and server exchange data.

 

 

2. Simulate the QQ chat function to achieve one-to-one chat between the client and the server

Server code:

Package demo.net; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import Java. util. chat;/*** simulates QQ chat: enables one-to-one chat between the client and the server. The client initiates a chat and displays the entered content on the server and the client, * Then, the server can enter information. The same information is displayed on the client and server * // server class public class chatserver {private int Port = 8189; // Default Server Port Public chatserver () {}// create public chatserver (INT port) {This. port = port;} // provide public void Service () {try {// establish a server connection serversocket Server = new serversocket (port ); // wait for the customer to connect to Socket socket = server. accept (); try {// datainputstreamdatainputstream in = new datainputstream (socket. getinputstream (); // dataoutputstreamdataoutputstream out = new dataoutputstream (socket. getoutputstream (); // obtain the scanner1_token = new token (system. in); While (true) {// read the information from the client string accpet = in. readutf (); system. out. println (accpet); string send = success. nextline (); system. out. println ("server:" + send); // send the Server Input to the client out. writeutf ("server:" + send) ;}} finally {// if the connection fails to be established, no socket is executed. close (); socket. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {New chatserver (). service ();}}


 

Client code:

Package demo.net; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import java.net. socket; import Java. util. optional;/*** note that the input and output streams datainputstream and dataoutputstream are used in pairs. It is best to use Word throttling * // client class public class chatclient {private string host = "localhost "; // connect to the local private int Port = 8189 by default; // The default connection port is 8189 public chatclient () {}// connect to the specified host and Port Public chatclient (string host, int port) {This. host = host; this. port = port;} public void chat () {try {// connect to the server Socket socket = new socket (host, Port ); try {// datainputstreamdatainputstream in = new datainputstream (socket. getinputstream (); // dataoutputstreamdataoutputstream out = new dataoutputstream (socket. getoutputstream (); // enter the standard input stream for decoration. in); While (true) {string send = success. nextline (); system. out. println ("client:" + send); // send the information obtained from the console to the server out. writeutf ("client:" + send); // read the information from the Server String accpet = in. readutf (); system. out. println (accpet) ;}} finally {socket. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {New chatclient (). chat ();}}


 

Running result:

Note that you must first open the server and then the client during running. The chat is initiated by the client. The green text in the running result indicates the input.

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.