Socket传输对象
2013-11-24 15:49
357 查看
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10000);
while (true) {
Socket socket = server.accept();
initSocket(socket);
}
}
private static void initSocket(final Socket socket) throws IOException {
new Thread(new Runnable() {
public void run() {
ObjectInputStream is = null;
ObjectOutputStream os = null;
try {
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
os = new ObjectOutputStream(socket.getOutputStream());
Object obj = is.readObject();
User user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
user.setName(user.getName() + "_new");
user.setPassword(user.getPassword() + "_new");
os.writeObject(user);
os.flush();
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
} catch(ClassNotFoundException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch(Exception ex) {}
try {
os.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}).start();
}
public static void main(String[] args) throws Exception {
Socket socket = null;
ObjectOutputStream os = null;
ObjectInputStream is = null;
try {
socket = new Socket("localhost", 10000);
os = new ObjectOutputStream(socket.getOutputStream());
User user = new User("user", "password");
os.writeObject(user);
os.flush();
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
Object obj = is.readObject();
if (obj != null) {
user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
}
} catch(IOException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch(Exception ex) {}
try {
os.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ServerSocket server = new ServerSocket(10000);
while (true) {
Socket socket = server.accept();
initSocket(socket);
}
}
private static void initSocket(final Socket socket) throws IOException {
new Thread(new Runnable() {
public void run() {
ObjectInputStream is = null;
ObjectOutputStream os = null;
try {
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
os = new ObjectOutputStream(socket.getOutputStream());
Object obj = is.readObject();
User user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
user.setName(user.getName() + "_new");
user.setPassword(user.getPassword() + "_new");
os.writeObject(user);
os.flush();
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
} catch(ClassNotFoundException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch(Exception ex) {}
try {
os.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}).start();
}
public static void main(String[] args) throws Exception {
Socket socket = null;
ObjectOutputStream os = null;
ObjectInputStream is = null;
try {
socket = new Socket("localhost", 10000);
os = new ObjectOutputStream(socket.getOutputStream());
User user = new User("user", "password");
os.writeObject(user);
os.flush();
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
Object obj = is.readObject();
if (obj != null) {
user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
}
} catch(IOException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch(Exception ex) {}
try {
os.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
相关文章推荐
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务1[使用IE浏览本页]
- Socket传输序列化对象
- socket传输类对象
- Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--前面6篇博文全部源代码下载地址
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- socket传输对象,这篇很失败,请看BLOG中另外一篇
- C# socket传输自定义对象
- socket对象的传输
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2[使用IE浏览本页]
- Java Socket 传输对象
- socket传输对象
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务3[聊天室][使用IE浏览本页]
- socket连接传输对象无响应
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务4[聊天室][使用IE浏览本页]
- 利用Socket实现多客户端传输对象和传输文件实现
- 使用Socket模拟简单的C/S通信,客户端之间数据传输采用对象流
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--类设计1
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- 关于socket使用Amf直接进行对象传输的