首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java套接字编程问题

java套接字编程问题
EN

Stack Overflow用户
提问于 2010-10-13 19:25:30
回答 2查看 2.5K关注 0票数 0

客户端:

代码语言:javascript
运行
复制
  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();
        }
    }

服务器:

代码语言:javascript
运行
复制
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)中,程序被working.If,将我的客户机程序放在其他系统中,假设172.16.3.30我得到异常(服务器仍然在172.16.3.219)。我是否应该改变socket()构造函数。如果问题出在构造器上,当客户端和服务器在不同的系统中时,建议我使用合适的构造器

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-13 19:43:25

代码看起来没问题;您是否在客户端或服务器上安装了防火墙?

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

编辑“连接被拒绝”意味着没有服务器在该服务器上的该端口上运行。运行netstat -tan (Linux)或netstat -tn (Windows)并使用LISTEN查找行。这些是正在运行的服务器进程。输出将如下所示:

代码语言:javascript
运行
复制
 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一起列出。如果不是,请尝试:

代码语言:javascript
运行
复制
 providerSocket = ServerSocket(2004, 10, InetAddress.getByName("0.0.0.0"));

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

票数 2
EN

Stack Overflow用户

发布于 2010-10-13 20:58:28

我猜(由于某种原因)服务器只监听回送ip地址。尝试绑定特定的ip地址,如下所示:

代码语言:javascript
运行
复制
//1. creating a server socket
providerSocket = ServerSocket(2004, 10, InetAddress.getByName("172.16.3.219"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3923093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档