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?

