Submitting and recieving classes through java

Hi,

I tried to write a program, which submitts and recieves java classes. This programm should work on a direct connection, where one client connects to another to submit the class with its contend.

Unfortunaly I recieve the following error:

java.netSocketException:Connection reset

Here is my code:

publicclass Submit

{

private InetAddress addr;

privateint port;

public Submit(String ip,int b)throws IOException, UnknownHostException

{

addr=InetAddress.getByName(ip);

port=b;

}

public Submit(InetAddress a,int b)throws IOException

{

addr=a;

port=b;

}

public InetAddress getInetAdress()

{

return addr;

}

publicvoid write(Object tosend)throws IOException

{

Socket so =new Socket(addr, port);

ObjectOutputStream out =new ObjectOutputStream(so.getOutputStream());

out.writeObject(tosend);

out.close();

so.close();

}

}

publicclass Recieve

{

private InetAddress addr;

privateint port;

public Recieve(String ip,int b)throws IOException, UnknownHostException

{

addr=InetAddress.getByName(ip);

port=b;

}

public Recieve(InetAddress a,int b)throws IOException

{

addr=a;

port=b;

}

public InetAddress getInetAdress()

{

return addr;

}

public Object get()throws IOException, ClassNotFoundException

{

Socket so=new Socket(addr, port);

ObjectInputStream in =new ObjectInputStream(so.getInputStream());

Object temp=in.readObject();

in.close();

so.close();

return temp;

}

}

Just assume that every necassary import is done. The two codes compile perfactly, but as soon as I try it with my computers with the following ip:

192.168.0.101

192.168.0.102

It throws the error:

java.netSocketException:Connection reset

This only happens on the Pc, who is actually recieving.

I also tried it with one computer via localhost/127.0.0.1/192.168.0.1 and i got:

java.net.ConnectException: Connection refused: connect

java.net.ConnectException: Connection refused: connect

In case you need the program, which starts the two classes just let me know, but it is not much of code, just putting the two classes in an while loop and leeting them repeat the connection.

I hope you can help me.

[4210 byte] By [betlora] at [2007-11-27 4:52:24]
# 1
You're going to need a ServerSocket that calls accept() somewhere in there. TCP/IP isn't a peer-to-peer protocol, it's a client-server protocol.
ejpa at 2007-7-12 10:06:24 > top of Java-index,Core,Core APIs...
# 2

Hi, thanks so far.

But unfortantly I still recieve an error:

java.net.SocketException: Connection reset

I edited the recieve class in the following way:

public class Recieve

{

private InetAddress addr;

private int port;

private boolean lock=false;

public Recieve(int b)

{

port=b;

}

public void reserve(InetAddress a)

{

addr=a;

lock=true;

}

public void reserve(String a) throws UnknownHostException

{

addr=InetAddress.getByName(a);

lock=true;

}

public InetAddress getInetAdress()

{

return addr;

}

public Object get() throws IOException, ClassNotFoundException

{

ServerSocket serso = new ServerSocket(port);

Socket so= serso.accept();

Object temp = new Object();

if(lock)

{

if(ifInetequal(so.getInetAddress()))

{

temp= getData(so);

}

}

else

{

temp= getData(so);

}

so.close();

serso.close();

return temp;

}

private boolean ifInetequal(InetAddress co) throws IOException

{

String tname1 =co.getHostAddress();

String tip1 =co.getHostName();

String tname2 =addr.getHostAddress();

String tip2 =addr.getHostName();

return (tname1.equals(tname2) && tip1.equals(tip2));

}

private Object getData(Socket so) throws IOException, ClassNotFoundException

{

ObjectInputStream in = new ObjectInputStream(so.getInputStream());

Object temp=in.readObject();

in.close();

return temp;

}

}

Just assume that all necassary import is done. What is causing the error massage? The program runs fine for about 3 times as soon as it tries to connect and recieve for the fourth it throws the error. In case that u need more code, just let me know.

betlora at 2007-7-12 10:06:24 > top of Java-index,Core,Core APIs...
# 3
Until you tell us which side gets the exception, and on what line of code, this is just a guessing game, but I think you need to read http://java.sun.com/docs/books/tutorial/networking/index.html first.
ejpa at 2007-7-12 10:06:24 > top of Java-index,Core,Core APIs...
# 4
Hi,it depends on the fact if the connection is just on one computer or on the two pc. In case that I am running it on two diffrent pcs it is allways the pc, which recieves the classes. In case I am running it on one Pc through localhost both Socket throw this exception.
betlora at 2007-7-12 10:06:24 > top of Java-index,Core,Core APIs...