基于TCP协议的Socket编程

1.单向通信实现

传输示意图

客户端程序

 1 import java.io.DataInputStream;
 2 import java.io.DataOutputStream;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 import java.net.Socket;
 7 
 8 public class clinet {
 9     
10     public static void main(String[] args) throws IOException {
11         //创建Socket对象
12         Socket clinet = new Socket("127.0.0.1",9999);
13         //获取输出流
14         DataOutputStream dos = new DataOutputStream(clinet.getOutputStream());
15         dos.writeUTF("hello word!");
16         //获取输入流
17         DataInputStream dis = new DataInputStream(clinet.getInputStream());
18         System.out.println(dis.readUTF());
19         //关闭流
20         if(dis != null) {
21             dis.close();
22         }
23         if(dos != null) {
24             dos.close();
25         }
26         if(clinet != null) {
27             clinet.close();
28         }
29     }
30 }
View Code

服务器程序

 1 import java.io.DataInputStream;
 2 import java.io.DataOutputStream;
 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 
11     public static void main(String[] args) throws IOException {
12         System.out.println("-----------服务器端已启动----------");
13         //创建ServerSocket对象
14         ServerSocket server = new ServerSocket(9999);
15         //监听是否有客户端请求连接
16         Socket clinet = server.accept();
17         //获取输入流
18         DataInputStream dis = new DataInputStream(clinet.getInputStream());
19         System.out.println(dis.readUTF());
20         //获取输出流
21         DataOutputStream dos = new DataOutputStream(clinet.getOutputStream());
22         dos.writeUTF("收到了");
23         //关闭流,关闭Socket
24         if(dos != null) {
25             dos.close();
26         }
27         if(dis != null) {
28             dis.close();
29         }
30         clinet.close();
31     }
32 
33 }
View Code

先启动服务器,再启动客户端

服务器:

客户端:

 

 

 

 

 

 

2.模拟用户登陆

服务器

 1 import java.io.DataOutputStream;
 2 import java.io.IOException;
 3 import java.io.ObjectInputStream;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 import javax.swing.plaf.synth.SynthSeparatorUI;
 8 
 9 import org.omg.PortableInterceptor.DISCARDING;
10 
11 public class Server2 {
12 
13     public static void main(String[] args) throws IOException, ClassNotFoundException {
14         System.out.println("------服务器端已启动----------");
15         //创建ServerSocket对象
16         ServerSocket server = new ServerSocket(10000);
17         Socket socket = server.accept();
18         //获取输入流(对象流)
19         ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
20         //对用户名和密码进行验证
21         User user = (User)ois.readObject();
22         System.out.println(socket.getInetAddress().getHostAddress()+"请求登陆:用户名"+user.getUsername()+"\t密码:"+user.getPassword());
23         String str = "";
24         if("chb".equals(user.getUsername())&&"123456".equals(user.getPassword())) {
25             str = "登陆成功";
26         }else {
27             str = "登陆失败";
28         }
29         //获取输出流(数据流)
30         DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
31         dos.writeUTF(str);
32         //关闭流
33         if(dos != null) {
34             dos.close();
35         }
36         if(ois != null) {
37             ois.close();
38         }
39         socket.close();
40     }
41 
42 }
View Code

客户端

 1 import java.io.DataInputStream;
 2 import java.io.IOException;
 3 import java.io.ObjectOutputStream;
 4 import java.net.Socket;
 5 import java.util.Scanner;
 6 
 7 public class Clint2 {
 8     
 9     public static void main(String[] args) throws IOException {
10         //(1)创建Socket对象,用于连接服务器
11         Socket clint = new Socket("localhost", 10000);
12         //(2)获取输出流(对象流)
13         ObjectOutputStream oos = new ObjectOutputStream(clint.getOutputStream());
14         //(3)创建User对象
15         User user = getUser();
16         //(4)User对象发生到服务器
17         oos.writeObject(user);
18         //(5)获取输入流(数据流)
19         DataInputStream dis = new DataInputStream(clint.getInputStream());
20         System.out.println(dis.readUTF());
21         if(dis != null) {
22             dis.close();
23         }
24         if(oos != null) {
25             oos.close();
26         }
27         clint.close();
28     }
29     public static User getUser() {
30         Scanner input = new Scanner(System.in);
31         System.out.println("请输入用户名:");
32         String username = input.next();
33         System.out.println("请输入密码:");
34         String password = input.next();
35         //封装成User对象
36         return new User(username, password);
37     }
38 }
View Code

 

3.UDP 通信_DatagramSocket 实现_客户咨询

1) 不需要利用 IO 流实现数据的传输

2) 每个数据发送单元被统一封装成数据包的方式,发送方将数据包发送到网络中,数据包在网络中去寻找他的目的地。

3) DatagramSocket:用于发送或接收数据包

4) DatagramPacket:数据包

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 
 5 public class Test1 {
 6     //接收方
 7             /** 一收,
 8              * 一发
 9              * @throws IOException 
10              */
11     public static void main(String[] args) throws IOException {
12         System.out.println("客服人员");
13         DatagramSocket ds=new DatagramSocket(9999);
14         //准 备接收数据
15         byte [] buf=new byte[1024];
16         //准 备数据报接收
17         DatagramPacket dp=new DatagramPacket(buf, buf.length);
18         
19         //接收
20         ds.receive(dp);
21         
22         //查看接收到的数据
23         String str=new String(dp.getData(),0,dp.getLength());
24         System.out.println("客户说:"+str);
25         
26         
27         /**回复数据*/
28         byte [] buf2="welcome to beijing".getBytes();//发生的内容 长度  地址 端口号
29         DatagramPacket dp2=new DatagramPacket(buf2, buf2.length, dp.getAddress(), dp.getPort());
30         ds.send(dp2);
31         //关闭
32         ds.close();
33         
34             
35     }
36 }
 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.util.Scanner;
 5 
 6 public class Test2 {
 7     //
 8             /** 一收,
 9              * 一发
10              * @throws IOException 
11              */
12     public static void main(String[] args) throws IOException {
13         Scanner input=new Scanner(System.in);
14         System.out.println("客服人员");
15         DatagramSocket ds=new DatagramSocket(9999);
16         while(true){
17             //准 备接收数据
18             byte [] buf=new byte[1024];
19             //准 备数据报接收
20             DatagramPacket dp=new DatagramPacket(buf, buf.length);
21             
22             //接收
23             ds.receive(dp);
24             
25             //查看接收到的数据
26             String str=new String(dp.getData(),0,dp.getLength());
27             System.out.println("客户说:"+str);
28             
29             String s=input.next();
30             /**回复数据*/
31             byte [] buf2=s.getBytes();
32             DatagramPacket dp2=new DatagramPacket(buf2, buf2.length, dp.getAddress(), dp.getPort());
33             ds.send(dp2);
34             if("bye".equals(s)){
35                 break;
36             }
37         }
38         //关闭
39             
40         ds.close();
41         
42             
43     }
44 }

 

posted @ 2019-04-29 19:22  北风吹沙  阅读(1531)  评论(0编辑  收藏  举报