TCP/IP四层网络模型:应用层、传输层、网络层和链路层。
//**************************************Socket 半双工通信***************************************************
Server:
import java.io.*;
import java.net.*;
/**
*@authorAdministrator
*
*/
//socket 服务端
publicclass MySocketServer {
/**
*@paramargs
*/
publicstaticvoid main(String[] args)
{
// TODO Auto-generated method stub
try
{
//创建 ServerSocket
ServerSocket ss=new ServerSocket(18888);
System.out.println("Listen port :18888 ...");
//监听端口(产生阻塞),直到某个客户端连接,一旦连接成功,就会得到Socket(管道)
//Socket 就就好像是一个管道
Socket s=ss.accept();
//连接成功读取客户端发送过来的信息
InputStreamReader isr =new InputStreamReader(s.getInputStream());
//转换成Buffer,方便读取
BufferedReader bs =new BufferedReader(isr);
while(true)
{
String clientRequest=bs.readLine();//逐行读取消息
System.out.println("Client Send Data:"+clientRequest);
//---------------服务器响应---------------------------
OutputStreamWriter osr =new OutputStreamWriter(s.getOutputStream());
BufferedWriter bw=new BufferedWriter(osr);
PrintWriter pw =new PrintWriter(bw,true);
if(!clientRequest.equals("End"))
{
pw.println("Server Receive Data:"+clientRequest);
}
else
{
pw.println("Good bye!");
s.close();
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Server 运行结果:
root@bt:~# javac MySocketServer.java
root@bt:~# java MySocketServer
Listen port :18888 ...
Client Send Data:1111111111111
Client Send Data:2222222222222
Client Send Data:AAAAAAAAAAAAA
Client Send Data:DKDKDKDKDKKDKSSSSS
Client Send Data:End
root@bt:~#
//***********************************************************************************************
Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
publicclass MySocketClient {
/**
*@paramargs
*/
publicstaticvoid main(String[] args)
{
// TODO Auto-generated method stub
try
{
//通过IP和端口主动连接服务器,连接成功产生一条管道
Socket s=new Socket("192.168.1.187",18888);
System.out.println("Connet ...");
//连接成功发送消息
OutputStreamWriter osw= new OutputStreamWriter(s.getOutputStream());
//转换成Buffer
BufferedWriter bw= new BufferedWriter(osw);
//创建自动刷新的新PrintWriter。
PrintWriter pw =new PrintWriter(bw,true);
System.out.println("Connet Success!");
//扫描控制台输入消息
Scanner sc=new Scanner(System.in);
String serverReponse="";
while(!serverReponse.equals("Good bye!"))
{
String consolveStr=sc.next(); //读取控制台信息
pw.println(consolveStr);//发送信息
//-------------------服务响应-------------
InputStream iStream=s.getInputStream();
InputStreamReader isr =new InputStreamReader(iStream);
BufferedReader br=new BufferedReader(isr);
//接收消息
serverReponse= br.readLine();
System.out.println("Server Send data :" +serverReponse);
if(consolveStr.equals("End"))
{
s.close();
break;
}
}
}
catch(Exception e)
{
e.printStackTrace(); }
}
}
Client 运行结果:
Connet ...
Connet Success!
1111111111111
Server Send data :Server Receive Data:1111111111111
2222222222222
Server Send data :Server Receive Data:2222222222222
AAAAAAAAAAAAA
Server Send data :Server Receive Data:AAAAAAAAAAAAA
DKDKDKDKDKKDKSSSSS
Server Send data :Server Receive Data:DKDKDKDKDKKDKSSSSS
End
Server Send data :Good bye!