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 {}}}}