Java网络编程:通过TCP协议传输一个对象到服务端[新]

昨天写了一个通过ByteArrayOutPutStream类来传输对象数据的方法,但是今天百度了一下才发现自己够傻逼了,因为Socket的getInputStream和getOutputStream返回的就是InputStream和OutputStream的对象,也就是说,可以不通过ByteArrayOutputStream类的实例化对象就可以将Socket的getOutputStream()方法返回的OutputStream实例化对象作为参数传递到ObjectOutputStream中,通过调用其writeObject()方法进行传输对象数据。并在接收端使用ObjcetInputStream类并传入Socket对象方法getIntputStream()返回的InputStream作为参数,然后通过readObject()方法获取传过来的对象。

相关文章:【Java网络编程:通过TCP协议传输一个对象到服务端】

以下为实例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package cn.com.lihuan.oicq;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
public class SendObject {
public static void main(String[] args) {
ServerForObject sfo = new ServerForObject(2541);
sfo.acceptObject();
new SocketForObject(“127.0.0.1”, 2541).sendObjcet(new Person(“李欢”, 25));
System.out.println(sfo.getObj().toString());
}
}
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return “Person [name=” + name + “, age=” + age + “]”;
}
}
class SocketForObject {
private int port;
private String ipAddress;
public SocketForObject() {
}
public SocketForObject(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public void sendObjcet(Object obj) {
try {
Socket s = new Socket(ipAddress, port);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); //将对象转换成字节码传输
oos.writeObject(obj); //开始传输
oos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerForObject {
private int port;
private Object obj = new Object();
public ServerForObject(int port) {
this.port = port;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Object getObj() {
return obj;
}
public void acceptObject() {
try {
final ServerSocket ss = new ServerSocket(port);
new Thread() {
public void run() {
try {
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); //获取接收到的对象字节码
obj = ois.readObject(); //将字节码转换为对象
ois.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

如果你有发现BUG,请留言反馈。