Day24## 知识点总结
1,网络编程 网络间通信
2,Socket
3,服务端:接收 客户端:发送
4,ip 端口号 ip:port 套接字
5,协议: tcp/ip udp arp http ftp …
tcp/ip 安全可靠协议 慢 三次握手四次挥手 不丢
udp 非安全可靠协议 快 没有 丢数据
通信代码:
sever:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class server {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
ServerSocket ss = new ServerSocket(1314);
Socket socket = ss.accept();
while(true){
InputStream fis = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
int i = bis.read(b);
String str = “”;
str += new String(b,0,i);
System.out.println(str);
java.io.OutputStream os = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
String s = sc.nextLine();
bos.write(s.getBytes());
bos.flush();
}
}
}
client:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import org.omg.CORBA.portable.InputStream;
public class client {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
Socket socket = new Socket(“192.168.69.22”,1314);
while (true){
OutputStream fos = socket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(fos);
String s=sc.nextLine();;
bos.write(s.getBytes());
bos.flush();
java.io.InputStream is = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte[] b = new byte[1024];
int i = bis.read(b);
String str = “”;
str += new String(b, 0, i);
System.out.println(“server:” + str);
}
}
}