10_1_网络
1. 客户端往服务器端写数据
TcpClient.java
package com.socket;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1", 8888);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("我是客户端,我要连接服务器,请接收。");
dos.close();
s.close();
}
}
TcpServer.java
package com.socket;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
while(true) {
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String ing = dis.readUTF();
System.out.println(ing);
dis.close();
s.close();
}
}
}
2. 客户端读取服务器数据
TcpServer.java
package com.socket;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
while(true) {
Socket s = ss.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("hello client" + "客户端ip为:" + s.getInetAddress() + "客户端端口为:"+s.getPort());
dos.close();
s.close();
}
}
}
TcpClient.java
package com.socket;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1", 8888);
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}
}
3. 客户端跟服务器双向读写
TcpServer.java
package com.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
while(true) {
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
String s1 = null;
if((s1 = dis.readUTF()) != null) {
System.out.println(s1);
}
dos.writeUTF("hello client" + "客户端ip为:" + s.getInetAddress() + "客户端端口为:"+s.getPort());
dis.close();
dos.close();
s.close();
}
}
}
TcpClient.java
package com.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1", 8888);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("hello server");
DataInputStream dis = new DataInputStream(s.getInputStream());
String s1 = null;
if((s1 = dis.readUTF()) != null) {
System.out.println(s1);
}
dos.close();
dis.close();
s.close();
}
}
4. 客户端、服务器端双向你说一句,我说一句
TcpServer.java
package com.socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("有客户端连接到服务器了。。。。。。。。。。");
BufferedReader ssin = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("clinet: " + ssin.readLine());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String line = sin.readLine();
while(!line.equals("bye")) {
System.out.println("server: " + line);
out.println(line);
out.flush();
System.out.println("clinet: " + ssin.readLine());
line = sin.readLine();
}
out.close();
s.close();
sin.close();
ssin.close();
}
}
TcpClient.java
package com.socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.lang.model.type.PrimitiveType;
public class TcpClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1", 8888);
System.out.println("客户端请求连接服务器");
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader ssin = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = sin.readLine();
while(!line.equals("bye")) {
System.out.println("client:" + line);
out.println(line);
out.flush();
System.out.println("server: " + ssin.readLine());
line = sin.readLine();
}
out.close();
s.close();
sin.close();
ssin.close();
}
}
5. UDP测试程序
TestUDPServer.java
package Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPServer {
public static void main(String[] args) throws IOException {
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
DatagramSocket ds = new DatagramSocket(3456);
while(true) {
ds.receive(dp);
System.out.println(new String(b, 0, dp.getLength()));
}
}
}
TestUDPClient.java
package Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPClient {
public static void main(String[] args) throws IOException {
byte[] b = new String("hello").getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, new InetSocketAddress("127.0.0.1", 3456));
DatagramSocket ds = new DatagramSocket(8888);
ds.send(dp);
ds.close();
}
}
6. UDP测试传递Long类型数据
TestUDPServer.java
package Test;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPServer {
public static void main(String[] args) throws IOException {
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
DatagramSocket ds = new DatagramSocket(3456);
while(true) {
ds.receive(dp);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream dis = new DataInputStream(bais);
Long n = dis.readLong();
System.out.println(n);
//System.out.println(new String(b, 0, dp.getLength()));
}
}
}
TestUDPClient.java
package Test;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPClient {
public static void main(String[] args) throws IOException {
long n = 10000L;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeLong(n);
byte[] b = bao.toByteArray();
DatagramPacket dp = new DatagramPacket(b, b.length, new InetSocketAddress("127.0.0.1", 3456));
DatagramSocket ds = new DatagramSocket(8888);
ds.send(dp);
ds.close();
}
}