0

i'm trying to send an object that will hold some information through ObjectOutputStream using sockets. when i call the method Client_Handler(String auth, String user) and send the object it works fine, but when i call it again it doesn't work. I want to be able to use the connection to send the object with different data inside it many times.

client:

public void Client_Handler(String auth, String user){
        try{
        socket2 = new Socket(serverAddress, 8080);
        User_Authorization us3 = new User_Authorization();
        ObjectOutputStream out2 = new ObjectOutputStream(socket2.getOutputStream());
        us3.set_name(user);
        us3.set_authorization(auth);
        out2.writeUnshared(us3);  
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        }  
}

server:

public class Conn implements Runnable{
private Socket socket;
public static ServerSocket server ;

public void go(){
    Thread r = new Thread(this);
        try{
                r.start();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        }  
}

@Override
public void run() {
    try{
    server = new ServerSocket(8080);
    socket = server.accept(); 
    while(true){
        Login.User_Authorization us = null;            
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        us = (Login.User_Authorization) in.readObject();
    System.out.println(us.get_name()+ "he's " +us.get_authorization());
   }
    }catch(Exception e){JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);}
}
Basil Basaif
  • 362
  • 8
  • 20

1 Answers1

1

when i call the method Client_Handler(String auth, String user) and send the object it works fine, but when i call it again it doesn't work.

Because, each time you are calling Client_Handler(String auth, String user) method you are trying to establish a new connection with Server via socket2 = new Socket(serverAddress, 8080); which is terminating the previous connection. That's why you are getting EOFException exception. You should create the socket2 object once before calling Client_Handler method. And while calling the Client_Handler method simply use the initialized Socket socket2.

You code could something be like this at client side:

Socket socket2;
ObjectOutputStream out2 = null;
public void mainMethod()
{
    try
    {
        socket2 = new Socket(serverAddress, 8080);  
        out2 = new ObjectOutputStream(socket2.getOutputStream());
        boolean canSend = true;
        while (canSend)
        {
            canSend = Client_Handler("authentication","user");
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        if (out2!=null)
        {
            try
            {
                out2.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
public boolean Client_Handler(String auth, String user)
{
    try
    {
        User_Authorization us3 = new User_Authorization();
        us3.set_name(user);
        us3.set_authorization(auth);
        out2.writeUnshared(us3);  
    }catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.ERROR_MESSAGE);
        return false;
    }  
    return true;
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • i created the 'socket2' object outside the method, but this time i got 'java.io.StreamCorruptedException: invalid type code: AC' when i call it again. – Basil Basaif Jun 26 '13 at 19:17
  • btw, can i use two sockets with different port numbers in the same program ?? – Basil Basaif Jun 26 '13 at 19:21
  • 1
    See my update.. And yes you can use two sockets with different port numbers in the same program. But you should call read/write operation on two sockets input/output stream in two separate threads.. because they are blocking operations and would block the thread ... – Vishal K Jun 26 '13 at 19:25
  • is the mainmethod the method that i'm ganna call everytime i want to send the object ?? – Basil Basaif Jun 26 '13 at 19:33
  • 1
    Yes..I have defined it according to my perception as you have not posted your all code.. – Vishal K Jun 26 '13 at 19:35
  • ouh, isee. then u must know that i'm calling the Client_Handler(String auth, String user) method from another class. – Basil Basaif Jun 26 '13 at 19:40
  • looks something like this, connn4.Client_Handler(user.get_File4_authorization(), user.get_name()); – Basil Basaif Jun 26 '13 at 19:41
  • i tried the code you submitted, but it didn't send anything :/ – Basil Basaif Jun 26 '13 at 19:44
  • 1
    Wherever you are calling this method..make sure that the object of `Socket` and `ObjectOutputStream` is created only **once** and that too before the `Client_Handler` method is called. I would suggest you to put your entire code of client side that you are using.... – Vishal K Jun 26 '13 at 19:44
  • sorry, i declared the socket2 twice. now it works fine, but it sends the data in a loop and i want it to only send once when i call it, and when i call it again it sends once as well. i tried to remove the loop or breaking it, but then it stopped working. – Basil Basaif Jun 26 '13 at 20:01
  • Then don't call `Client_Handler` in loop – Vishal K Jun 26 '13 at 20:03
  • Thank you, it worked. all i had to do is not creating 'socket2' and 'ObjectOutputStream' everytime i call the 'Client_Handler' method – Basil Basaif Jun 26 '13 at 20:15