Java如何用TCP接受除String,byte,int外的自定义的类数据? ( 积分: 200 )

  • 主题发起人 主题发起人 liangbowen
  • 开始时间 开始时间
L

liangbowen

Unregistered / Unconfirmed
GUEST, unregistred user!
how to use tcp socket to receive a class instance
-------------------------------------------------------------------------------
Author: liangbowen Jul 6, 2005 1:09 AM
I've created a class instance c2smsg, then
use PS.println(c2smsg) to send the instance to server.
But how can I receive the class and read the class members at the server side?
socket.getInputStream() can only receive String, byte[], or int...
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: nickelb@ck Jul 6, 2005 1:15 AM (reply 1 of 3)
Write the socket input/output streams in ObjectInput/OutputStreams
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: nickelb@ck Jul 6, 2005 1:16 AM (reply 2 of 3)
Damn, not 'Write', but 'Wrap'. Sorry
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: liangbowen Jul 6, 2005 7:29 PM (reply 3 of 3)
To be clear, here is the sample code, i wrote a TestClass, and i want to send it and read it from the recieving side.
class TestClass{
String testSting = "i love you";
int port = 1024;
}
//send side
TestClass tc = new TestClass();
PrintWriter out=new PrintWriter(socket.getOutputStream());
...
out.println(tc);
//i am not sure if this is right
//send side end

//receive side
BufferedReader buf =new BufferedReader(new InputStreamReader(client.getInputStream()));
//then
what can ido
to buf to read out TestClass members? buf.readLine()?
//receive side end
 
how to use tcp socket to receive a class instance
-------------------------------------------------------------------------------
Author: liangbowen Jul 6, 2005 1:09 AM
I've created a class instance c2smsg, then
use PS.println(c2smsg) to send the instance to server.
But how can I receive the class and read the class members at the server side?
socket.getInputStream() can only receive String, byte[], or int...
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: nickelb@ck Jul 6, 2005 1:15 AM (reply 1 of 3)
Write the socket input/output streams in ObjectInput/OutputStreams
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: nickelb@ck Jul 6, 2005 1:16 AM (reply 2 of 3)
Damn, not 'Write', but 'Wrap'. Sorry
-------------------------------------------------------------------------------
Re: how to use tcp socket to receive a class instance
Author: liangbowen Jul 6, 2005 7:29 PM (reply 3 of 3)
To be clear, here is the sample code, i wrote a TestClass, and i want to send it and read it from the recieving side.
class TestClass{
String testSting = "i love you";
int port = 1024;
}
//send side
TestClass tc = new TestClass();
PrintWriter out=new PrintWriter(socket.getOutputStream());
...
out.println(tc);
//i am not sure if this is right
//send side end

//receive side
BufferedReader buf =new BufferedReader(new InputStreamReader(client.getInputStream()));
//then
what can ido
to buf to read out TestClass members? buf.readLine()?
//receive side end
 
有没人回答,这个问题太久了,我要清理我的待答问题,要散分拉。
 
谢谢楼主
 
这应该是序列化/反序列化吧.这个应该是JAVA基本的冬冬.我想基本的书籍都应该对此有所交待的.
不过我个人对JAVA不在行.
 
//server
//*********************************************************
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
//主程序一直处于监听状态,有连接则启动一个线程进行处理,以实现多个客户端
public class SocketSrv {
private static final int PORT = 10015;
private static final int THREADCOUNT = 10;
private ServerSocket ss;
private boolean listening = true;
public SocketSrv() {
Init();// 初始化
lisn();// 启动监听
}
public void Init() {
try {
ss = new ServerSocket(PORT, THREADCOUNT);
} catch (IOException ie) {
System.out.println("无法在"
+ PORT + "端口监听");
ie.printStackTrace();
}
}
public void lisn() {
try {
while (listening) {
System.out.print("按 q 退出监听:");
listening = System.in.read() != 'q';
if (!listening)
break;
// 为每一个accept开启一个线程,来对客户端进行响应
new Thread(new dialogserve(ss.accept())).start();
}
ss.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
public static void main(String args[]) {
new SocketSrv();
}
}
// 以下为会话主程序
// 应该特别注意,如果客户端先关闭,会话socket中可能抛出socketexception:connection reset
// 这应该在程序中进行处理,这也是较易忽略的问题.
class dialogserve implements Runnable {
private Socket s;
private InputStream in;
private ObjectInputStream ois = null;
private String rev, temp;
private byte b[];
private int len;
private SerializeClass sc = null;
public dialogserve(Socket ss) {
s = ss;
System.out.println("accept: host="
+ s.getRemoteSocketAddress().toString());
b = new byte[1024];
try {
in = s.getInputStream();
} catch (IOException ie) {
ie.printStackTrace();
}
rev = "";
}
public void run() {
try {
while (s.isConnected() == true) {
/*
* if ((len = in.read(b)) != -1) { temp = new String(b, 0, len);
rev += temp;
* System.out.print(rev);
temp = null;
Thread.sleep(1000);
}
*/
/**
* 测试序传送列化对象
*/
ois = new ObjectInputStream(in);
sc = (SerializeClass)(ois.readObject());
System.out.println(sc.toString());
}
in.close();
s.close();
System.out.println("会话socket已断开!");
} catch (SocketException se) {
System.out.println("客户端已断开!");
System.exit(0);
} catch(ClassNotFoundException ce){
System.out.println("未知对象");
System.exit(0);
}
catch (IOException io) {
io.printStackTrace();
System.exit(0);
}
}
}

//client
//*********************************************************
//以下为客户端主程序
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class SocketClt {
private static final String HOST = "localhost";
private static final int PORT = 10015;
private static final int TIMEOUT = 2000;
private Socket con;// 客户端连接socket
private OutputStream out;
private ObjectOutputStream oos;
private String sen;
private byte b[];

private SerializeClass sc = null;
public SocketClt() {
clientInit();
}
public void clientInit() {
try {
con = new Socket(HOST, PORT);
con.setSoTimeout(TIMEOUT);
/* b = new byte[1024];
OutputStream out = con.getOutputStream();
sen = "hello serve,以TCP方式发送数据!";
b = sen.getBytes();
out.write(b);*/
/**
* 测试序传送列化对象
*/
sc = new SerializeClass("SerializeClass", 123);
oos = new ObjectOutputStream(con.getOutputStream());
oos.writeObject(sc);

out.flush();
out.close();
con.close();
} catch (IOException ie) {
ie.toString();
}
}
public static void main(String args[]) {
new SocketClt();
}
}
//Serialize class
//*********************************************************
import java.io.*;
public class SerializeClass implements Serializable {
private static final long serialVersionUID = -7466172181301918356L;
private String str = null;
private int port = 0;
public SerializeClass(String s, int p) {
str = s;
port = 0;
}
public String toString() {
return new String("str:"
+ str + '/n' + "port:"
+ port);
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
 
谢谢 wlmmlw
 
后退
顶部