Operating environment: Eclipse | | MyEclipse
Package socaket;
These classes are all placed under the Socaket package.
This is the server-side message sending class
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.net.Socket;
Import Java.util.Scanner;
public class Serverchatsend extends Thread {
Server-Side Send thread
Socket SSK = null;
constructor function
Serverchatsend (Socket SK) {
This.ssk = SK;
}
public void Serverchatsend () {
try {
Socket SK = Ssk.accept ();
PrintWriter pw = new PrintWriter (Ssk.getoutputstream ());
Scanner scan = new Scanner (system.in);
String meg = null;
while (meg = Scan.nextline ()) = null) {
Pw.println (MEG);
Pw.flush ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
@Override
public void Run () {
Serverchatsend ();
}
}
---------------------------------------------------------------
Package socaket;
Server-side Message reception
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
Server-side Receive thread
public class Serverchatreceive extends Thread {
Private Socket SK = null;
Constructors
Public serverchatreceive (Socket SK) {
this.sk = SK;
}
public void Serverchatreceive () {
try {
BufferedReader bfr = new BufferedReader (New InputStreamReader (
Sk.getinputstream ()));
String Meg =null;
while (meg = Bfr.readline ()) = null) {
System.out.println ("Client:" + meg);
}
} catch (IOException e) {
E.printstacktrace ();
}
}
public void Serverfilereceive () {
}
@Override
public void Run () {
Serverchatreceive ();
}
}
--------------------------------------------------------
Client message Sending
Package socaket;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.util.Scanner;
Client Send Thread
public class Clientchatsend extends Thread {
Private Socket Sk=null;
constructor function
Clientchatsend (Socket SK) {
This.sk=sk;
}
public void Clientchatsend () {
try {
Scanner scan = new Scanner (system.in);
PrintWriter pw = new PrintWriter (Sk.getoutputstream ());
String meg = null;
while (true) {
if (Meg =scan.nextline ()) = null) {
Pw.println (MEG);
Pw.flush ();
}
}
} catch (Unknownhostexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
@Override
public void Run () {
Clientchatsend ();
}
}
--------------------------------------------------------
Client Message Reception
Package socaket;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Client Receive Thread
public class Clientchatreceive extends thread{
Private Socket Sk=null;
Clientchatreceive (Socket SK) {
This.sk=sk;
}
Receiving of messages
public void Clientchatreceive () {
try {
BufferedReader bfr=new BufferedReader (New InputStreamReader (Sk.getinputstream ()));
String Meg=null;
while ((Meg=bfr.readline ())!=null) {
System.out.println ("Server:" +meg);
}
} catch (Unknownhostexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
@Override
public void Run () {
Clientchatreceive ();
}
}
-------------------------------------------------------
Service-side Main method
Package socaket;
Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Serverchat {
Service-Side Main program
public static void Main (string[] args) {
try {
Establish a connection
ServerSocket SSK = new ServerSocket (1234);
Socket SK = Ssk.accept ();
Creating objects
Serverchatreceive scr = new serverchatreceive (SK);
Serverchatsend SCS = new Serverchatsend (SK);
Start thread
Scr.start ();
Scs.start ();
SYSTEM.OUT.PRINTLN ("The server-side program is started! ");
} catch (IOException e) {
E.printstacktrace ();
}
}
}
-----------------------------------------------------------
Client Main method
Package socaket;
Import java.io.IOException;
Import Java.net.Socket;
Import java.net.UnknownHostException;
public class Clientchat {
Client Main Program
public static void Main (string[] args) {
try {
Connection test for this machine
Socket SK = new socket ("127.0.0.1", 1234);
Clientchatreceive ccr=new clientchatreceive (SK);
Clientchatsend ccs=new Clientchatsend (SK);
Thread Start-up
Ccr.start ();
Ccs.start ();
SYSTEM.OUT.PRINTLN ("The client program has started! ");
} catch (Unknownhostexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
-------------------------------------
Note: We run the server first run the main method, and then run the client Main method can send each other message, here the program is tested on this machine, if you want to test with others, a running client, a running service side, change the corresponding IP and port number can be >_< note Oh, Regardless of the test, open the server side is right
Write the client and server program with Socaket to send messages to each other