Socket编程全部,TCP/IP

一、TCP/IP 通信通信,必须要有IP地址,必须要有相同的套接字(socket)
步骤:
1、寻址定位:
通过IP地址 找到主机
通过port (端口号)(0-65535) 找到程序
查看主机的端口号:cmd netstat -ano
SQL 端口号:3306
我们自己的:8080之后
本机IP 127.0.0.1

2、通信(协议)
TCP/IP包含:
TCP 可靠(三次握手)B是否能接收,B是否接收到,B返回信息给A
TUP 不可靠,无状态,速度快,视频一般用这种

二、套接字 socket
socket 应用程序访问TCP/IP 的调用

socket编程:
①、服务器端:
1、建立服务端某一个端口监听
ServerSocket server = new ServerSocket(8080);
2、准备解释客服端请求:等待-准备
Socket socket = server.accept();
3、创建输入流:
BufferedReader br = new Buff..(new InputS..R..(socket.getInputStream));
PrintWriter pw = new PrintWriter(Socket.getOutputStream(),true);
4、关闭流,关闭socket,关闭server
br.close();socket.close();pw.colse();

②、客服端:少了第一步ServerSocket
1、向服务器发请求
Socket socket = new Socket("127.0.0.1",8080);
2、创建输入输出流:
BufferedReader br = new Bu..R..(new InputS..R(socket.getInputStream))
3、关闭流,socket,server

 

没有一直监听的:

java tcpip报文 java tcp/ip socket编程_java

java tcpip报文 java tcp/ip socket编程_客户端_02

1 package com.socket;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.Socket;
 7 import java.net.UnknownHostException;
 8 
 9 import javax.swing.JOptionPane;
10 
11 /**
12  * Date: 2016-3-19-上午11:18:18
13  * Class_name: Client.java
14  * Package_name: com.lovo.demo_18
15  * Author: ZhangYue
16  * Description: 
17  */
18 public class Client {
19     
20     
21     public Client(){
22         try {
23             
24             String input_info = JOptionPane.showInputDialog(null,"请输入请求信息");
25             
26             Socket socket = new Socket("127.0.0.1", 8090);
27             
28             //客户端向服务器发送请求
29             OutputStream out = socket.getOutputStream();
30             out.write(input_info.getBytes());
31             
32             //客户端接收服务器的响应
33             InputStream in = socket.getInputStream();
34             byte [] b = new byte[1024];
35             
36             System.out.println(b.length);
37             
38             in.read(b);
39             
40             System.out.println("客户端接收到服务器端的响应信息:" + new String(b).trim());
41             
42             out.close();
43             in.close();
44             socket.close();
45         } catch (UnknownHostException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         } catch (IOException e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         }
52     }
53     
54     
55     public static void main(String[] args) {
56         new Client();
57     }
58 
59 }

Client

java tcpip报文 java tcp/ip socket编程_java

java tcpip报文 java tcp/ip socket编程_客户端_02

1 package com.socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.InputStreamReader;
 8 import java.io.OutputStream;
 9 import java.net.ServerSocket;
10 import java.net.Socket;
11 
12 /**
13  * Date: 2016-3-19-上午11:10:15 Class_name: Server.java Package_name:
14  * com.lovo.demo_18 Author: ZhangYue Description:
15  */
16 public class Server {
17 
18     public Server() {
19 
20         try {
21             ServerSocket server = new ServerSocket(8090);
22 
23             System.out.println("服务器启动,等待客户端请求...");
24 
25 //            while (true) {
26                 Socket socket = server.accept();// 监听客户端的请求
27 
28                 // 服务器接收客户端的请求
29                 InputStream in = socket.getInputStream();
30                 byte[] by = new byte[1024];
31                 in.read(by);
32                 
33                 System.out.println("服务器端接收到了客户端的请求信息:" + new String(by).trim());
34 
35                 // 服务器响应信息返回给客户端
36                 OutputStream out = socket.getOutputStream();
37 
38                 out.write("Hello".getBytes());
39 
40                 in.close();
41                 out.close();
42                 socket.close();
43                 server.close();
44 //            }
45         } catch (IOException e) {
46             e.printStackTrace();
47         }
48     }
49 
50     public static void main(String[] args) {
51         new Server();
52     }
53 
54 }

Server

 

服务器一直监听的:

java tcpip报文 java tcp/ip socket编程_java

java tcpip报文 java tcp/ip socket编程_客户端_02

1 package com.soket.thread;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.Socket;
 7 
 8 public class Client {
 9     
10     public Client(){
11         Socket socket = null;
12         try {
13             socket = new Socket("127.0.0.1",8088);
14             InputStream in = socket.getInputStream();
15             OutputStream out = socket.getOutputStream();
16             
17             String request = "Hello, I'm Client";
18             
19             out.write(request.getBytes());
20             out.flush();
21             System.out.println("客户端发送请求:" + request);
22             
23             byte[] by = new byte[1024];
24             in.read(by);
25             String str = new String(by).trim();
26             System.out.println("客户端接收到服务器响应: " + str);
27             
28             
29             out.close();
30             in.close();
31         } catch (IOException e) {
32             e.printStackTrace();
33         }finally{
34             try {
35                 socket.close();
36             } catch (IOException e) {
37                 e.printStackTrace();
38             }
39         }
40     }
41 
42     /**
43      * @param args
44      */
45     public static void main(String[] args) {
46         // TODO Auto-generated method stub
47         new Client();
48     }
49 
50 }

Client

java tcpip报文 java tcp/ip socket编程_java

java tcpip报文 java tcp/ip socket编程_客户端_02

1 package com.soket.thread;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 public class Server {
10     public static boolean isRun = true;
11     public Server(){
12         try {
13             ServerSocket s = new ServerSocket(8088);
14             
15             System.out.println("服务器启动,准备接收客户端请求...");
16             
17             while(true){//利用while循环设置服务器一直监听
18                 Socket socket = s.accept();
19                 /**
20                  * 1. 利用线程,可支持多线程
21                     new SocketThread(socket);
22                 */
23                 
24                 /**
25                  * 2. 直接书写在里面
26                  * */
27                 try {
28                     InputStream in = socket.getInputStream();
29                     OutputStream out = socket.getOutputStream();
30                     
31                     byte[] by = new byte[1024];
32                     in.read(by);
33                     String str = new String(by).trim();
34                     System.out.println("服务器接收到客户端请求:" + str);
35                     
36                     out.write("Hello, I'm Server".getBytes());
37                     
38                     out.flush();
39                     out.close();
40                     in.close();
41                 } catch (IOException e) {
42                     e.printStackTrace();
43                 }finally{
44                     try {
45                         socket.close();
46                     } catch (IOException e) {
47                         e.printStackTrace();
48                     }
49                 }
50                 
51             }
52         } catch (IOException e) {
53             e.printStackTrace();
54         }
55     }
56 
57     public static void main(String[] args) {
58         Server s = new Server();
59     }
60 
61 }

Server

 

加入线程的服务器:

java tcpip报文 java tcp/ip socket编程_java

java tcpip报文 java tcp/ip socket编程_客户端_02

1 package com.soket.thread;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.Socket;
 7 
 8 public class SocketThread extends Thread{
 9     private Socket socket;
10     public SocketThread(Socket socket) {
11         this.socket = socket;
12         this.start();
13     }
14     @Override
15     public void run() {
16         try {
17             InputStream in = socket.getInputStream();
18             OutputStream out = socket.getOutputStream();
19             
20             byte[] by = new byte[1024];
21             in.read(by);
22             String str = new String(by).trim();
23             System.out.println("服务器接收到客户端请求:" + str);
24             
25             out.write("Hello, I'm Server".getBytes());
26             
27             out.flush();
28             out.close();
29             in.close();
30         } catch (IOException e) {
31             e.printStackTrace();
32         }finally{
33             try {
34                 socket.close();
35             } catch (IOException e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40 
41 }

SocketThread