I have a client/server program(that use sockets) written in java. It has a multiple functions that establishing connections. For example:
public static void some_sender(byte[] x0, byte[] x1) //he is a server
{
ServerSocket s=new ServerSocket(8888);
s.setSoTimeout(10000);
Socket incoming=s.accept();
ObjectOutputStream oos = new ObjectOutputStream(incoming.getOutputStream());
ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());
//some sending and receiving
some_sender0(a);
}
public static void some_sender0(int a)
{
ServerSocket s=new ServerSocket(8888);
s.setSoTimeout(10000);
Socket incoming=s.accept();
ObjectOutputStream oos = new ObjectOutputStream(incoming.getOutputStream());
ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());
//some sending and receiving
}
The same thing I have with client()
public static byte[] some_receiver(byte b)
{
Socket s=new Socket("127.0.0.1", 8888);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ios=new ObjectInputStream(s.getInputStream());
//some sending and receiving
byte[] t=some_receiver0(c);
return m;
}
public static byte[] some_receiver0(byte c)
{
Socket s=new Socket("127.0.0.1", 8888);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ios=new ObjectInputStream(s.getInputStream());
//some sending and receiving
return y;
}
My question is how to execute(initialize server)
ServerSocket s=new ServerSocket(8888);
s.setSoTimeout(10000);
Socket incoming=s.accept();
ObjectOutputStream oos = new ObjectOutputStream(incoming.getOutputStream());
ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());
and (client)
Socket s=new Socket("127.0.0.1", 8888);
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ios=new ObjectInputStream(s.getInputStream());
only once and use it in all nested functions.
Thank you in advance.