Error by submitting objects with socket

Hallo,

I allways get the following error massage:

Error:

java.net.ConnectException: Connection timed out: connect

Exception in thread "Thread-0" java.lang.NullPointerException

at com.Receiver.read(Receiver.java:36)

at Input.write(Kommunicator.java:111)

at Input.run(Kommunicator.java:137)

Error:

java.net.ConnectException: Connection timed out: connect

Here is my code:

import com.*;

import TerminalIO.*;

import java.util.*;

import java.io.*;

import java.net.*;

publicclass Kommunicator

{

publicstaticvoid main(String [] args){

KeyboardReader inread =new KeyboardReader();

int port = inread.readInt();

String ip =inread.readLine();

Emitter out =new Emitter(ip, port);

Input in=new Input(new Receiver(ip, port));

try{

in.start();

boolean flag=true;

while(flag){

out.open();

if(line.equalsIgnoreCase("\\quit")){

out.write("User left");

in.interrupt();

flag=false;

}

else{

out.write(line);

}

out.close();

}

}

catch(IOException e){

System.out.println("Error:\n"+e.toString());

}

}

}

class Inputextends Thread

{

private Receiver out;

public Input(Receiver a){out=a;}

privatevoid open(){

try{

out.open();

super.start();

}

catch(IOException e)

{

System.out.println("Error:\n"+e.toString());

}

}

privatevoid close(){

try{

out.close();

}

catch(IOException e)

{

System.out.println("Error:\n"+e.toString());

}

}

privatevoid write(){

try{

Object temp=out.read();

if(!temp.equals(null))

{

String line=temp.toString();

do

{

System.out.println("Other:"+line);

line=out.read().toString();

}while(!line.equals(""));

}

}

catch(IOException e)

{

System.out.println("Error:\n"+e.toString());

}

catch(ClassNotFoundException e)

{

System.out.println("Error:\n"+e.toString());

}

}

publicvoid run()

{

open();

write();

close();

}

}

package com;

import java.net.*;

import java.io.*;

publicclass Receiver

{

private String ip;

privateint port;

private ObjectInputStream ois;

private Socket sock;

public Receiver(String a,int b)

{

ip=a;

port=b;

}

publicvoid open()throws IOException

{

sock =new Socket(InetAddress.getByName(ip), port);

ois =new ObjectInputStream(sock.getInputStream());

sock.setSoTimeout(3000);

}

publicvoid close()throws IOException

{

ois.close();

sock.close();

}

public Object read()throws IOException,ClassNotFoundException

{

Object temp=null;

temp=ois.readObject();

return temp;

}

publicint getPort()

{return port;}

public String getIp()

{return ip;}

}

package com;

import java.io.*;

import java.net.*;

publicclass Emitter

{

private String ip;

privateint port;

private Socket sock;

private ObjectOutputStream oos;

public Emitter(String a,int b)

{

ip=a;

port=b;

}

public String getIp()

{return ip;}

publicint getPort()

{return port;}

publicvoid open()throws IOException

{

sock =new Socket(InetAddress.getByName(ip),port);

oos =new ObjectOutputStream(sock.getOutputStream());

sock.setSoTimeout(3000);

}

publicvoid write(Object a)throws IOException

{oos.writeObject(a);}

publicvoid close()throws IOException

{

oos.close();

sock.close();

}

}

I hope you can help me with my problem and I hope you can answer my questions;

1) is it a general problem with socket, that it is not allowed to submit objects on a permanent way?

2) is it there another way how to submit object?

3) can the problem be caused through trying to submit packeges in a lan?

[9774 byte] By [betlora] at [2007-11-26 14:00:19]
# 1
You might try looking here http://java.sun.com/developer/technicalArticles/ALT/sockets/index.htmlgood luck
jditria at 2007-7-8 1:41:45 > top of Java-index,Core,Core APIs...
# 2
The Input class has no constructor, then you are not initializing the Receiver called out. It gives a null pointer exception when tryin g to use the methods of a null reference.
MelGohana at 2007-7-8 1:41:45 > top of Java-index,Core,Core APIs...
# 3

1. When you get an exception trying to construct any object, it is poor practice to print and ignore it.

2. It is invalid to continue as though the object had been constructed.

3. In this case you failed to connect but then continued to the read method using a null socket reference.

4. You need to restructure your code so that it reacts correctly to exceptions.

5. Then you need to solve the question of why the connect failed.

ejpa at 2007-7-8 1:41:45 > top of Java-index,Core,Core APIs...