0

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.

4

2 回答 2

0

我建议将它们放入用作服务器/客户端控制器的 Singleton 类中。在适当的时候初始化它们并确保它们具有所有必要的 getter(而不是 setter)和实用方法,以便您的其他类可以使用套接字流。

于 2013-04-29T20:25:46.283 回答
0

谢谢 Sotirios Delimanolis 的建议。我决定更OO。创建了一个有两个方法的类。一个用于启动 Socket 连接并分配流,第二个用于关闭 evething。例如

import java.io.*;
import java.net.*;

public class NetworkTransfers { 

public Socket s = null;
public ObjectOutputStream oos = null;
public ObjectInputStream ios = null;

public void activateNetwork()
{
    try {
        s = new Socket("IP-address", Port);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        oos = new ObjectOutputStream(s.getOutputStream());
        ios = new ObjectInputStream(s.getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

}

public void closeConnections()
{
    try {
        s.close();
        ios.close();
        oos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

在服务器端几乎相同。希望它会对某人充满希望。

于 2013-09-10T17:53:35.413 回答