1.tcp的實(shí)現(xiàn)
socket的服務(wù)端
/**
* @Project: 3.DistributedProject
* @description: socket的服務(wù)端
* @author: sunkang 一定在流寫的時(shí)候flush
* 這里printWrite 必須要用println ,因?yàn)樵趯懭氲臅r(shí)候加上了一個(gè)換行符號
*
* @create: 2018-06-27 23:00
* @ModificationHistory who when What
**/
public class ServerSocketDemo {
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(8083);
Socket socket = server.accept(); //阻塞過程
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:" + is.readLine()); //拿到客戶端的信息
String line = sin.readLine();
while (!line.equals("bye")) {
os.println(line);//輸出數(shù)據(jù)
os.flush();
System.out.println("Server:" + line);
System.out.println("Client:" + is.readLine());
line = sin.readLine();
}
os.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
socket的客戶端
/**
* @Project: 3.DistributedProject
* @description: socket的客戶端
* @author: sunkang
* @create: 2018-06-27 23:00
* @ModificationHistory who when What
**/
public class SocketDemo {
public static void main(String[] args) {
try {
Socket socket=new Socket("127.0.0.1",8083);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=sin.readLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("Client:"+line);
System.out.println("Server:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
2.udp的實(shí)現(xiàn)
udp 服務(wù)端的實(shí)現(xiàn)
/**
* @Project: 3.DistributedProject
* @description: udp 服務(wù)端的實(shí)現(xiàn)
* @author: sunkang
* @create: 2018-06-28 19:05
* @ModificationHistory who when What
**/
public class UdpServerDemo {
public static void main(String[] args) {
try {
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
DatagramSocket datagramSocket = new DatagramSocket(2345);
datagramSocket.receive(datagramPacket);
//打印數(shù)據(jù)包的內(nèi)容
System.out.println(new String(bytes,0,datagramPacket.getLength()));
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
udp 客戶端的實(shí)現(xiàn)
/**
* @Project: 3.DistributedProject
* @description: udp 客戶端的實(shí)現(xiàn)
* @author: sunkang
* @create: 2018-06-28 19:00
* @ModificationHistory who when What
**/
public class UdpClientDemo {
public static void main(String[] args) {
InetAddress inetAddress= null;
try {
inetAddress = InetAddress.getByName("localhost");
byte[] data = "sunkang hello".getBytes();
//數(shù)據(jù)包攜帶了包體的字節(jié)數(shù)據(jù)和ip地址以及端口號
DatagramPacket datagramPacket = new DatagramPacket(data,data.length,inetAddress,2345);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(datagramPacket);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}