Based on Java Socket client and server chat prototype

Source: Internet
Author: User

Server. Java

Package Org. sunchao; import Java. io. bufferedreader; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import java.net. serversocket; import java.net. socket;/*** socket server code ** @ author administrator **/public class server {Private Static final int Port = 8888; private Socket socket; public static void main (string [] ARGs) {server Server = new server (); connectthread connect = server. new connectthread (); New thread (CONNECT ). start ();}/*** server waits for the connection thread ** @ author administrator **/class connectthread implements runnable {@ overridepublic void run () {try {system. out. println ("Waiting client to connect... "); serversocket = new serversocket (port); socket = serversocket. accept (); // start the thread that receives and sends messages after the socket connection. The thread that receives and sends messages will receive ETHREAD receive = new receivethread (); sendthread send = new sendthread (); New thread (receive ). start (); New thread (send ). start ();} catch (exception e) {e. printstacktrace ();} finally {}}/*** server receiving message thread ** @ author administrator **/class receivethread implements runnable {@ overridepublic void run () {try {string Info; inputstream is = socket. getinputstream (); datainputstream Dis = new datainputstream (is); While (true) {info = dis. readutf (); system. out. println ("client:" + info); If (info. equals ("bye") {break ;}} catch (exception e) {e. printstacktrace ();} finally {}}/*** server sending message thread ** @ author administrator **/class sendthread implements runnable {@ overridepublic void run () {try {outputstream OS = socket. getoutputstream (); dataoutputstream dos = new dataoutputstream (OS); inputstreamreader ISR = new inputstreamreader (system. in); // read data from the keyboard bufferedreader BR = new bufferedreader (ISR); string Info; while (true) {info = BR. readline (); dos. writeutf (Info); If (info. equals ("bye") {break ;}} catch (exception e) {e. printstacktrace () ;}finally {}}}}

Client. Java

Package Org. sunchao; import Java. io. bufferedreader; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import java.net. socket;/*** Socket Client code ** @ author administrator **/public class client {Private Static final string IP = "192.168.0.107"; Private Static final int Port = 8888; private Socket socket; public static void main (string [] ARGs) {Client client = new client (); connectthread connect = client. new connectthread (); New thread (CONNECT ). start ();}/*** client connects to the server thread ** @ author administrator **/class connectthread implements runnable {@ overridepublic void run () {try {socket = new socket (IP, Port); // After the socket is connected, start the thread that receives and sends messages, and then execute receivethread receive = new etethread (); sendthread send = new sendthread (); New thread (receive ). start (); New thread (send ). start ();} catch (exception e) {e. printstacktrace ();} finally {}}/*** client receiving message thread ** @ author administrator **/class receivethread implements runnable {@ overridepublic void run () {try {string Info; inputstream is = socket. getinputstream (); datainputstream Dis = new datainputstream (is); While (true) {info = dis. readutf (); system. out. println ("server:" + info); If (info. equals ("bye") {break ;}} catch (exception e) {e. printstacktrace ();} finally {}}/*** client message sending thread ** @ author administrator **/class sendthread implements runnable {@ overridepublic void run () {try {outputstream OS = socket. getoutputstream (); dataoutputstream dos = new dataoutputstream (OS); inputstreamreader ISR = new inputstreamreader (system. in); // read data from the keyboard bufferedreader BR = new bufferedreader (ISR); string Info; while (true) {info = BR. readline (); dos. writeutf (Info); If (info. equals ("bye") {break ;}} catch (exception e) {e. printstacktrace () ;}finally {}}}}

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.