目录
1.TCP-对象
2.UDP
1.TCP-对象
利用TCP传输对象信息,需要对对象进行实例化
User类:
package dh09.demo02;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String pwd;
public User() {
}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
服务器端:
package dh09.demo02;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public class Demo01 {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8090);
Socket socket = serverSocket.accept();
InputStream iStream = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(iStream);
User user = (User) ois.readObject();
System.out.println("客户端说:"+user.getName()+"-"+user.getPwd());
//通过输出流给客户端一个响应
OutputStream os = socket.getOutputStream();
String reply = "欢迎登陆";
os.write(reply.getBytes());
os.close();
ois.close();
iStream.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端:
package dh09.demo02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
//客户端
public class Test01 {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8090);
OutputStream oStream = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(oStream);
User user = new User("admin","123456");
oos.writeObject(user);
socket.shutdownOutput();
InputStream iStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
String reply;
while ((reply = br.readLine())!=null) {
System.out.println("服务器响应说:"+reply);
}
br.close();
iStream.close();
oos.close();
oStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.UDP
package dh09.UDPDemo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ReceiveMessageDemo {
public static void main(String[] args) {
//接收数据
//1.创建DatagramSocket对象
try {
DatagramSocket dSocket = new DatagramSocket(10086);
//2.接收数据包
byte[] b = new byte[1024];
DatagramPacket dPacket = new DatagramPacket(b,b.length);
dSocket.receive(dPacket);
//3.解析数据包
byte[] data = dPacket.getData();
int len = dPacket.getLength();
InetAddress address = dPacket.getAddress();
int port = dPacket.getPort();
System.out.println("接收到数据:"+new String(data,0,len));
System.out.println("该数据是从:"+address+",这台电脑中的:"+port+"这个端口号发出的");
//4.释放资源
dSocket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发送端:
package dh09.UDPDemo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SendMessageDemo {
public static void main(String[] args) {
try {
//1.创建DatagramSocket对象
DatagramSocket dSocket = new DatagramSocket();
//2.打包数据
String string = "你好";
byte[] b = string.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10086;
DatagramPacket dPacket = new DatagramPacket(b, b.length,address,port);
//3.发送数据
dSocket.send(dPacket);
//4.释放资源
dSocket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}