(一)了解Socket
传输层有两种通信模式,一种是面向连接的可靠的通信,另一种是无连接的不可靠的通信,TCP是一种面向连接的保证可靠传输的协议,
通过TCP协议传输,得到的是一个顺序的无差错的数据流。而UDP是无连接的不可靠的通信。
socket 是基于TCP协议的一种可靠的通信,它是应用层和传输层之间的一个通信接口。
发送方和接收方的成对的两个socket之间必须建 立连接,以便在TCP协议的基础上进行通信,当一个socket(通常都是server socket服务器)等待建立连接时,
另一个socket(客户端)可以要求进行连接,一旦这两个socket连接起来,它们就可以进行双向数据传输,双方都可以进行发送 或接收操作。
(二)循环服务器模式
程序功能为:
(1) 服务器可以接受任何客户的连接
(2) 服务器在同一时刻只能与一个客户通信,直到该客户退出才可以接收下一个客户
(3) 客户程序使用命令行参数指定服务器地址
(4) 客户端输入的文本都发送给服务器
(5) 客户使用输入end时停止发送,关闭连接
代码:
public class Service1 {
static int num = 1 ;
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(10086); //创建绑定到特定端口的服务器套接字。
System.out.println("....Service Start!");
while(true){
Socket client=server.accept(); //等待客户连接请求
System.out.println("Client"+num+" Connect!");
num++ ;
//读取客户端数据流
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
// 获取写往客户端的输出流,true表示自动刷新
PrintWriter out=new PrintWriter(client.getOutputStream(),true);
while(true){
String str=in.readLine();
System.out.println("Client: "+str);
out.println("has receive....");
out.flush();
if(str.equals("end")){
System.out.println("Client Close!") ;
break ;
}
}
client.close();
}
}
}
(三) 多线程服务器
程序功能为:
(1) 服务器可以接受任何客户的连接
(2) 客户程序使用命令行参数指定服务器地址
(3) 客户端输入的文本都发送给服务器
(4) 客户使用输入end时停止发送,关闭连接
(5)服务器能同时连接多个客户端
import java.io.*;
import java.net.*;
public class Service1 {
static int num = 1 ;
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(10086); //创建绑定到特定端口的服务器套接字。
System.out.println("....Service Start!");
while(true){
Socket client=server.accept(); //等待客户连接请求
S_Thread thread = new S_Thread(client) ; //开启一个线程处理连接
thread.start() ;
}
}
}
public class S_Thread extends Thread{
Socket client ;
public S_Thread(Socket client){
this.client = client ;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;// 获取写往客户端的输出流,true表示自动刷新
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
String str = null;
try {
str = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Client: "+str);
out.println("has receive....");
out.flush();
if(str.equals("end")){
System.out.println("Client Close!") ;
break ;
}
}
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(四)客户端
public class Client {
Socket so ;
BufferedReader netin ;
BufferedReader usein;
PrintWriter out;
public static void main(String[] args) throws Exception {
Client client = new Client(); //创建一个客户端
client.runMain() ;
}
public void runMain() {
try {
//创建连接指定服务器和端口的Socket
so= new Socket("localhost",10086);
System.out.println("...Connect");
//创建键盘输入的数据流
usein = new BufferedReader(new InputStreamReader(System.in));
//创建发送到服务器端的数据流
out= new PrintWriter(so.getOutputStream());
boolean flag=true;
while(flag) {
String theLine=usein.readLine();
//通过流发送信息给服务器端
out.println( theLine);
out.flush();
BufferedReader in=new BufferedReader(new InputStreamReader(so.getInputStream()));
String str=in.readLine();
System.out.println("Server:"+str);
if(theLine.equals("end")){
so.close() ;
break ;
}
}
}
catch(Exception e) {
}
}
}
(五)
详细可运行代码:
(http://download.csdn.net/download/hdfgo/10186745)