[Java] Socket/ServerSocket/Thread console chat program-any chat between two users

Source: Internet
Author: User

// Server code
Import java. io .*;
Import java.net .*;

Public class ServerTest3 {
Public static void main (String [] args ){
Try {
// Set the sever Link
ServerSocket s = new ServerSocket (8888 );
Socket s1 = s. accept (); // sever waiting for Link

// Instantiate the output stream
OutputStream OS = s1.getOutputStream ();
DataOutputStream dos = new DataOutputStream (OS );
// Instantiate the input stream
InputStream is = s1.getInputStream ();
DataInputStream dis = new DataInputStream (is );
// Instantiate the objects of two threads
Thread msr = new MyServerReader (dis );
Thread msw = new MyServerWriter (dos );
// Start the thread
Msr. start ();
Msw. start ();
}
// Capture exceptions
Catch (SocketException e ){
System. out. println (e );
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process for receiving and reading data
Class MyServerReader extends Thread {
Private DataInputStream dis;
Public MyServerReader (DataInputStream dis ){
This. dis = dis;
}
Public void run (){
String msg;
Try {
While (true ){
Msg = dis. readUTF ();
System. out. println ("the other party said:" + msg );
If (msg. equals ("bye ")){
System. out. println ("the other party goes offline, the program exits ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process to write and send data
Class MyServerWriter extends Thread {
Private DataOutputStream dos;
Public MyServerWriter (DataOutputStream dos ){
This. dos = dos;
}
Public void run (){
InputStreamReader isr = new InputStreamReader (System. in );
BufferedReader br = new BufferedReader (isr );
String msg;
Try {
While (true ){
Msg = br. readLine ();
Dos. writeUTF (msg );
If (msg. equals ("bye ")){
System. out. println ("deprecate yourself, exit the program ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Client code
Import java. io .*;
Import java.net .*;

Public class ClientTest3 {
Public static void main (String [] args ){
Try {
// Link the server
Socket s1 = new Socket ("127.0.0.1", 8888 );
// Instantiate the input stream
InputStream is = s1.getInputStream ();
DataInputStream dis = new DataInputStream (is );
// Instantiate the output stream
OutputStream OS = s1.getOutputStream ();
DataOutputStream dos = new DataOutputStream (OS );
// Instantiate two processes
Thread mcr = new MyClientReader (dis );
Thread mcw = new MyClientWriter (dos );
// Start two processes
Mcr. start ();
Mcw. start ();
// Capture exceptions
} Catch (SocketException e ){
System. out. println (e );
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process for receiving and reading data
Class MyClientReader extends Thread {
Private DataInputStream dis;
Public MyClientReader (DataInputStream dis ){
This. dis = dis;
}
@ Override
Public void run (){
String msg;
Try {
While (true ){
Msg = dis. readUTF ();
System. out. println ("the other party said:" + msg );
If (msg. equals ("bye ")){
System. out. println ("the other party goes offline, the program exits ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process to write and send data
Class MyClientWriter extends Thread {
Private DataOutputStream dos;
Public MyClientWriter (DataOutputStream dos ){
This. dos = dos;
}
@ Override
Public void run (){
InputStreamReader isr = new InputStreamReader (System. in );
BufferedReader br = new BufferedReader (isr );
String msg;
Try {
While (true ){
Msg = br. readLine ();
Dos. writeUTF (msg );
If (msg. equals ("bye ")){
System. out. println ("deprecate yourself, exit the program ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
Open two consoles, first run the server code, then run the client code, and then the two ends can make free calls

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.