java socket编程问题

时间:2010-10-13 11:25:30

标签: java

客户:

  import java.io.*;
  import java.net.*;
  public class Requester
  {
        Socket requestSocket;
        ObjectOutputStream out;
        ObjectInputStream in;
        String message;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        Requester(){}
        void run() throws IOException
        {
            try{
                //1. creating a socket to connect to the server
                requestSocket = new Socket("172.16.3.219", 2004);
                System.out.println("Connected to localhost in port 2004");

                //2. get Input and Output streams
                out = new ObjectOutputStream(requestSocket.getOutputStream());
                out.flush();
                in = new ObjectInputStream(requestSocket.getInputStream());

                //3: Communicating with the server
                do{
                     try{
                        message = (String)in.readObject();
                        System.out.println("server>" + message);
                        message=br.readLine();
                        sendMessage(message);
                     }
                        catch(ClassNotFoundException classNot)
                        {
                           System.err.println("data received in unknown format");
                    }
                }while(!message.equals("bye"));
               }
               catch(UnknownHostException unknownHost)
               {
                System.err.println("You are trying to connect to an unknown host!");
               }
               catch(IOException ioException)
               {
                ioException.printStackTrace();
               }
               finally
               {
                //4: Closing connection
                try{
                    in.close();
                    out.close();
                    requestSocket.close();
                   }
                  catch(IOException ioException)
                  {
                    ioException.printStackTrace();
                  }
               }
        }
        void sendMessage(String msg)
        {
            try{
                out.writeObject(msg);
                out.flush();
                System.out.println("client1>" + msg);
               }
              catch(IOException ioException)
              {
                ioException.printStackTrace();
              }
        }
        public static void main(String args[]) throws IOException
        {
            Requester client = new Requester();
            client.run();
        }
    }

服务器:

import java.io.*;
import java.net.*;
public class Provider
{
    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Provider(){}
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    void run()
    {
        try{
            //1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);

            //2. Wait for connection
            System.out.println("Waiting for connection");

              connection = providerSocket.accept();
              System.out.println("Connection received from" + connection.getInetAddress().getHostName());

            //3. get Input and Output streams
                out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");

            //4. The two parts communicate via the input and output streams
            do
            {
               try
               {
                    message = (String)in.readObject();
                    System.out.println("client>" + message);

                    if (message.equals("bye"))
                    {
                        sendMessage("bye");
                    }
                        else
                    {
                        message=br.readLine();
                        sendMessage(message);
                    }
              }
              catch(ClassNotFoundException classnot)
              {
                System.err.println("Data received in unknown format");
              }
            }while(!message.equals("bye"));
        }
        catch(IOException ioException)
        {
            ioException.printStackTrace();
        }
        finally
        {
            //4: Closing connection
            try{
                        in.close();
                        out.close();
                        providerSocket.close();
               }
              catch(IOException ioException)
              {
                            ioException.printStackTrace();
              }
            }
    }

        void sendMessage(String msg)
    {
        try
        {
            out.writeObject(msg);
            out.flush();
            System.out.println("server>" + msg);
        }
        catch(IOException ioException)
        {
            ioException.printStackTrace();
        }
    }
    public static void main(String args[])throws IOException
    {
       Provider server = new Provider();
       while(true)
       {
        server.run();
       }
    }
}                                                                                                                                                                                                               

服务器程序位于172.16.3.219。如果服务器和客户端都在一个系统(172.16.3.219)中,则程序正在运行。如果我的客户端程序位于某个其他系统中,请让我们说172.16.3.30我得到例外(仍然是服务器在172.16.3.219)。我应该更改socket()构造函数。如果问题是构造函数建议我在客户端和服务器位于不同系统时使用合适的构造函数

2 个答案:

答案 0 :(得分:2)

代码看起来不错;你在客户端或服务器上有防火墙吗?

我正在尝试测试发送邮件的代码。为此,我安装了本地邮件服务器但无法连接:管理员已禁用SMTP端口以阻止垃圾邮件发送者。

[编辑]“拒绝连接”表示该服务器上的该端口上没有运行任何服务器。运行netstat -tan(Linux)或netstat -tn(Windows)并查找LISTEN行。这些是运行的服务器进程。输出将如下所示:

 Active Internet connections (servers and established)
 Proto Recv-Q Send-Q Local Address           Foreign Address         State      
 tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN     
 tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

有趣的部分是“本地地址”。它包含服务器正在侦听的网络接口(IP地址)。第一个条目侦听所有网络接口,而第二个条目附加到“localhost”(也称为“本地环回”)。它将忽略来自LAN的任何连接尝试。

确保您的服务器列有0.0.0.0:2004。如果不是,请尝试:

 providerSocket = ServerSocket(2004, 10, InetAddress.getByName("0.0.0.0"));

如果不起作用,请尝试使用服务器网卡的公共IP地址。

答案 1 :(得分:1)

我猜(由于某种原因)服务器只监听环回IP地址。尝试绑定特定的IP地址,例如:

//1. creating a server socket
providerSocket = ServerSocket(2004, 10, InetAddress.getByName("172.16.3.219"));