简单的TCP流程
如图:
案例:
服务器端
01 |
public class SimpleTcpServer {
|
02 |
public static void main(String[] args) {
|
03 |
try {
|
04 |
//套接字服务器,相当于中心号码
|
05 |
ServerSocket ss = new ServerSocket( 8001 );
|
06 |
//等待被呼叫,建立一个Socket
|
07 |
Socket s = ss.accept();
|
08 |
//从建立的Socket中建立字节输入输出流,读取或发送数据
|
09 |
InputStream ips = s.getInputStream();
|
10 |
OutputStream ops = s.getOutputStream();
|
11 |
//以字节的形式接收数据
|
12 |
byte [] buf = new byte [ 1024 ];
|
13 |
int length = ips.read(buf); //听
|
14 |
System.out.println( "服务器接收:" + new String(buf, 0 ,length));
|
15 |
//以字节的形式发送一条固定语句(写字节) |
16 |
String sendStr = "服务器发送的数据" ;
|
17 |
ops.write(sendStr.getBytes());
|
18 |
//关闭
|
19 |
ips.close();
|
20 |
ops.close();
|
21 |
s.close();
|
22 |
ss.close();
|
23 |
} catch (Exception ex){
|
24 |
ex.printStackTrace();
|
25 |
}
|
26 |
|
27 |
}
|
28 |
} |
客户端
01 |
public class SimpleTcpClient {
|
02 |
public static void main(String[] args) {
|
03 |
try {
|
04 |
//创建客户端套接字指定连接的IP地址和端口号
|
05 |
Socket s = new Socket(InetAddress.getByName( "127.0.0.1" ), 8001 );
|
06 |
//创建输入输出流
|
07 |
InputStream ips = s.getInputStream();
|
08 |
OutputStream ops = s.getOutputStream();
|
09 |
//往套接字中写数据(字节)
|
10 |
String sendStr = "客户端发送的数据" ;
|
11 |
ops.write(sendStr.getBytes());
|
12 |
//接收数据并显示出来
|
13 |
byte [] buf = new byte [ 1024 ];
|
14 |
int length = ips.read(buf);
|
15 |
System.out.println( "客户端接收:" + new String(buf, 0 ,length));
|
16 |
//关闭
|
17 |
ips.close();
|
18 |
ops.close();
|
19 |
s.close();
|
20 |
} catch (Exception ex){
|
21 |
ex.printStackTrace();
|
22 |
}
|
23 |
|
24 |
}
|
25 |
} |
TCP变成总结
服务器端必须先启动
服务器指定了端口号后,客户端创建套接字时必须使用相同的端口号
客户端需知道服务器的IP地址
客户端连接到服务器后,通过输入输出流进行信息交流,实际是在Socket中进行读写数据
演示服务器的连接请求队列的特性。
01 |
import java.net.*;
|
02 |
public class Client {
|
03 |
public static void main(String args[]) throws Exception{
|
04 |
final int length= 100 ;
|
05 |
String host= "localhost" ;
|
06 |
int port= 8000 ;
|
07 |
Socket[] sockets= new Socket[length];
|
08 |
for ( int i= 0 ;i<length;i++){ //试图建立100次连接
|
09 |
sockets[i]= new Socket(host, port);
|
10 |
System.out.println( "第" +(i+ 1 )+ "次连接成功" );
|
11 |
}
|
12 |
Thread.sleep( 3000 );
|
13 |
for ( int i= 0 ;i<length;i++){
|
14 |
sockets[i].close(); //断开连接
|
15 |
}
|
16 |
}
|
17 |
} |
01 |
import java.io.*;
|
02 |
import java.net.*;
|
03 |
public class Server {
|
04 |
private int port= 8000 ;
|
05 |
private ServerSocket serverSocket;
|
06 |
public Server() throws IOException {
|
07 |
serverSocket = new ServerSocket(port, 3 ); //连接请求队列的长度为3
|
08 |
System.out.println( "服务器启动" );
|
09 |
}
|
10 |
public void service() {
|
11 |
while ( true ) {
|
12 |
Socket socket= null ;
|
13 |
try {
|
14 |
socket = serverSocket.accept(); //从连接请求队列中取出一个连接
|
15 |
System.out.println( "New connection accepted " +
|
16 |
socket.getInetAddress() + ":" +socket.getPort());
|
17 |
} catch (IOException e) {
|
18 |
e.printStackTrace();
|
19 |
} finally {
|
20 |
try {
|
21 |
if (socket!= null )socket.close();
|
22 |
} catch (IOException e) {e.printStackTrace();}
|
23 |
}
|
24 |
}
|
25 |
}
|
26 |
public static void main(String args[]) throws Exception {
|
27 |
Server server= new Server();
|
28 |
Thread.sleep( 60000 * 10 ); //睡眠10分钟
|
29 |
//server.service();
|
30 |
}
|
31 |
} |