综述:
分别创建两个Java项目,一个是客户端,一个作为服务器端(另一个客户端)。
在客户端程序的主线程中分别创建两个线程,一个是负责实时监听服务器端的输入,另一个线程负责向服务器端实时写入数据。
服务器端同理于客户端程序的设计。
客户端程序:
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client {
public static void main(String args[]){
Socket ClientSocket=null;
DataInputStream dis=null;
DataOutputStream dos=null;
DetectServer detect=null;
try{
ClientSocket=new Socket();
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress IsocketAddress=new InetSocketAddress(address,2010);
ClientSocket.connect(IsocketAddress);
dis=new DataInputStream(ClientSocket.getInputStream());
dos=new DataOutputStream(ClientSocket.getOutputStream());
huifu call=new huifu(dos);
call.start();
detect=new DetectServer(dis);
detect.start();
}
catch(IOException e){
System.out.println("客服端连接服务器发生异常");
System.exit(1);
}
}
}
<span style="font-size:18px;color:#3333ff;">//用此客户端的线程监测服务器是否向客户端发送了数据,如果发送了,那么随时读取所返回的数据。
//主线程实现想发就发,想发什么发什么。该线程负责无论在何时只要服务器传来了数据就能实时读出来</span>
class DetectServer extends Thread{
DataInputStream read=null;
DetectServer(DataInputStream temp)
{
read=temp;
}
public void run()//从输入流中读取服务器返回的数据
{
while(true)
{
try{
String temp=read.readUTF();
System.out.println("服务器:"+temp);
}
catch(IOException e)
{
System.out.println("从服务器读数据时发现与服务器断开.......");
System.exit(1);
}
}
}
}
<span style="font-size:18px;color:#cc33cc;">//在该线程实现客户端“想发就发,想发什么发什么,想啥时候发就啥时候发”</span>
class huifu extends Thread{
Scanner reader=new Scanner(System.in);
DataOutputStream write=null;
huifu(DataOutputStream write){
this.write=write;
}
public void run(){
while(reader.hasNextLine())
{
try{
String s=reader.nextLine();//实现想发什么就发什么
write.writeUTF(s);
}
catch(IOException e){
System.out.println("向服务器写数据时候发现与服务器断开");
System.exit(1);
}
}
}
}
服务器端程序:
也可以把服务器端当成另一个客户端,就是通信双方中的一方。
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Server {
public static void main(String args[]){
Socket serverSocket=null;<span style="font-size:14px;color:#009900;">//当主机做服务器时候做套接字使用</span>
DataInputStream dis=null;
DataOutputStream dos=null;
ServerSocket ser=null;
try{
ser=new ServerSocket(2010);<span style="font-size:14px;color:#cc33cc;">//监听对象不能够重复创建,即不能再一个循环内出现多次new ServerSocket(2010);</span>
serverSocket=ser.accept();
dos=new DataOutputStream(serverSocket.getOutputStream());
dis=new DataInputStream(serverSocket.getInputStream());
huifu call=new huifu(dos);
call.start();
DetectClient dc=new DetectClient(dis);
dc.start();
}
catch(IOException e)
{
System.out.println("监听端口时出现异常");
System.exit(1);
}
}
}
<span style="font-size:18px;color:#6633ff;">//该线程实现让服务端随时向客户端发送数据</span>
class huifu extends Thread{
DataOutputStream write=null;
Scanner sc=new Scanner(System.in);
huifu(DataOutputStream write)
{
this.write=write;
}
public void run()
{
while(true)
{
String str=sc.nextLine();
try{
write.writeUTF(str);
}
catch(IOException e){
System.out.println("服务器输入时候出现异常");
System.exit(1);
}
}
}
}
<span style="font-size:18px;color:#cc33cc;">
<span style="background-color: rgb(255, 255, 255);">//该线程负责随时监测客户端,只要客户端向服务端发送了数据那就会被该线程实时显示出来</span></span>
class DetectClient extends Thread{<span style="font-size:14px;color:#3333ff;">//负责监听客户端的线程类</span>
DataInputStream read=null;
DetectClient(DataInputStream temp)
{
read=temp;
}
public void run()
{
while(true)
{
try{
String temp=read.readUTF();
System.out.println("客户:"+temp);
}
catch(IOException e)
{
System.out.println("从服务器读数据时发现与服务器断开.......");
System.exit(1);
}
}
}
}