服务器客户端初始化(只有一次)java

时间:2013-04-29 20:17:28

标签: java sockets network-programming

我有一个用java编写的客户端/服务器程序(使用套接字)。它具有多个建立连接的功能。例如:

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
    }

与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;
}

我的问题是如何执行(初始化服务器)

 ServerSocket s=new ServerSocket(8888);
 s.setSoTimeout(10000);
 Socket incoming=s.accept();
 ObjectOutputStream oos = new ObjectOutputStream(incoming.getOutputStream());
 ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());

和(客户)

 Socket s=new Socket("127.0.0.1", 8888);
 ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
 ObjectInputStream ios=new ObjectInputStream(s.getInputStream());

只能在所有嵌套函数中使用它。
提前谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

感谢Sotirios Delimanolis的建议。我决定更多OO。创建了一个有两种方法的类。一个用于启动Socket连接并分配流,另一个用于关闭evething。 E.g。

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();
    }

}

}

在服务器端非常相似。 希望对某人有希望。