java 中socket传输对象的问题,望高手解答
publicclassMainServer{//服务器类ServerSocketserver;Socketclient;Npnp;//自己定义的一个数据类MainServ...
public class MainServer {//服务器类
ServerSocket server;
Socket client;
Np np;//自己定义的一个数据类
MainServer()
{
try {
server = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("服务器已启动!");
while(true)
{
try {
client=server.accept();
System.out.println("client="+client.getLocalAddress());
System.out.println("服务器接收客户端....");
new Thread(new checkLogin(client)).start();//运行一个线程
System.out.println("线程已运行");
} catch (IOException e) {
e.printStackTrace();
}
}
}
class checkLogin implements Runnable
{ ObjectInputStream ois;
ObjectOutputStream oos;
Socket socket;
public checkLogin(Socket socket) {
try {
ois=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("checkLogin is running...");
while(np!=null)
{
try {
np=(Np) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("np已经接受");
System.out.println("Np是:"+np.name+""+np.password);
}
}
}
public static void main(String[] args) {
new MainServer();
}
}
public class Login {//客户端类,发送了一个Np对象
Socket socket;
ObjectInputStream ois;
ObjectOutputStream oos;
Np np;
Login()
{
try {
socket=new Socket("121.250.216.47",8888);
ois=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
try {
oos.writeObject(new Np("001","001"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new Login();
}
}
public class Np implements Serializable{//只知道要实现这个接口,这是我的数据类,就想传这个类的对象
private static final long serialVersionUID = 1L;
String name;
String password;
Np(String name,String password)
{
this.name=name;
this.password=password;
}
} 展开
ServerSocket server;
Socket client;
Np np;//自己定义的一个数据类
MainServer()
{
try {
server = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("服务器已启动!");
while(true)
{
try {
client=server.accept();
System.out.println("client="+client.getLocalAddress());
System.out.println("服务器接收客户端....");
new Thread(new checkLogin(client)).start();//运行一个线程
System.out.println("线程已运行");
} catch (IOException e) {
e.printStackTrace();
}
}
}
class checkLogin implements Runnable
{ ObjectInputStream ois;
ObjectOutputStream oos;
Socket socket;
public checkLogin(Socket socket) {
try {
ois=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
System.out.println("checkLogin is running...");
while(np!=null)
{
try {
np=(Np) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("np已经接受");
System.out.println("Np是:"+np.name+""+np.password);
}
}
}
public static void main(String[] args) {
new MainServer();
}
}
public class Login {//客户端类,发送了一个Np对象
Socket socket;
ObjectInputStream ois;
ObjectOutputStream oos;
Np np;
Login()
{
try {
socket=new Socket("121.250.216.47",8888);
ois=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
oos=new ObjectOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
try {
oos.writeObject(new Np("001","001"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new Login();
}
}
public class Np implements Serializable{//只知道要实现这个接口,这是我的数据类,就想传这个类的对象
private static final long serialVersionUID = 1L;
String name;
String password;
Np(String name,String password)
{
this.name=name;
this.password=password;
}
} 展开
5个回答
展开全部
你现在是什么问题?
下边是例子,可以传送对象,没有实现序列化接口的类,是不能通过
ObjectInputStream
ObjectOutputStream
来读写的。
------------------------------------------------------------------------------------------------------------------------
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8090);
Socket client = server.accept();
ObjectInputStream oin = new ObjectInputStream(client.getInputStream());
Np obj = (Np) oin.readObject();
oin.close();
client.close();
server.close();
System.out.println(obj.name);
System.out.println(obj.password);
}
}
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket client = new Socket("localhost", 8090);
ObjectOutputStream out = new ObjectOutputStream(client
.getOutputStream());
out.writeObject(new Np("user", "password"));
out.flush();
out.close();
client.close();
}
}
class Np implements Serializable {
String name;
String password;
Np(String name, String password) {
this.name = name;
this.password = password;
}
}
下边是例子,可以传送对象,没有实现序列化接口的类,是不能通过
ObjectInputStream
ObjectOutputStream
来读写的。
------------------------------------------------------------------------------------------------------------------------
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8090);
Socket client = server.accept();
ObjectInputStream oin = new ObjectInputStream(client.getInputStream());
Np obj = (Np) oin.readObject();
oin.close();
client.close();
server.close();
System.out.println(obj.name);
System.out.println(obj.password);
}
}
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket client = new Socket("localhost", 8090);
ObjectOutputStream out = new ObjectOutputStream(client
.getOutputStream());
out.writeObject(new Np("user", "password"));
out.flush();
out.close();
client.close();
}
}
class Np implements Serializable {
String name;
String password;
Np(String name, String password) {
this.name = name;
this.password = password;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关闭,在关闭之前flush试一下。。。要有个良好的关闭对象的习惯。。。其实这种问题我以前也遇到过,通常都是忘了刷空,你的代码我大概浏览了下,试一下flush,我好久没用过网络编程了,希望这个建议会使你不再纠结
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请问该如何解决这个问题? 你试试把do while 换成while用,我不推荐用do while。do while是只先做后加。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
while(np!=null)这里好像有问题。线程执行之时它还未曾拿到任何值,但是又不能读承续读传过来的数据了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-04-28
展开全部
代码写的不规范,没有关闭流吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询