简体   繁体   English

套接字编程-客户端(Linux),服务器(Windows)

[英]Socket programming - Client (linux), server (Windows)

I'm trying to create simple socket application using sockets to send stream from linux (64x ArchLinux) to server (Windows XP). 我正在尝试使用套接字创建简单的套接字应用程序,以将流从Linux(64x ArchLinux)发送到服务器(Windows XP)。

Code I'm using I found on the internet, just to check if it is working. 我在互联网上找到的我正在使用的代码,只是用来检查它是否有效。 What is interesting the code works perfectly if I'm using Windows XP (server) and Win 8 (client), but when client is on ArchLinux it does not work. 有趣的是,如果我使用Windows XP(服务器)和Win 8(客户端),则代码可以完美地工作,但是当客户端在ArchLinux上时,它将无法正常工作。 Is there some special way to connect Windows-Linux ? 有没有连接Windows-Linux的特殊方法?

Server.java 服务器.java

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

class Server_pzm {
   public static void main(String args[]) {
      String data = "Toobie ornaught toobie";
      try {
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.print("Server has connected!\n");
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: '" + data + "'\n");
         out.print(data);
         out.close();
         skt.close();
         srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

Client.java 客户端.java

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

class Client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("192.168.224.78", 1234);
         BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));
         System.out.print("Received string: '");

         // while (!in.ready()) {} line removed
         System.out.println(in.readLine());  


         System.out.print("'\n");
         in.close();
      }
      /* lines removed catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      } */

      // added exception handling
      catch(UnknownHostException e) {
         e.printStackTrace();
      }
      catch (IOException e){
         e.printStackTrace();
      }

   }
}

EDIT 编辑

Sorry, I did not specify what I meant by not working. 抱歉,我没有指定不工作的意思。 I meant I got an exception which later prints System.out.print("Whoops! It didn't work!\\n"); 我的意思是我遇到了一个异常,该异常后来打印了System.out.print(“哇!它不起作用!\\ n”); as in the catch blok. 就像大块头一样。 Win 8 and Arch Linux are installed on the same laptop, while the code is on my dropbox folder in both systems (so the code is the same) - I will post the actual exception, after I get back to my laptop Win 8和Arch Linux安装在同一台笔记本电脑上,而代码在两个系统中的我的Dropbox文件夹中(因此代码是相同的)-在我回到笔记本电脑后,我将发布实际的异常

EDIT 2: 编辑2:

I updated code and this is exception I got: 我更新了代码,这是我得到的例外:

java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at java.net.Socket.connect(Socket.java:528)
        at java.net.Socket.<init>(Socket.java:425)
        at java.net.Socket.<init>(Socket.java:208)

java.net.ConnectException: Connection refused java.net.ConnectException:连接被拒绝

This has two possible meanings. 这有两个可能的含义。

  1. There is nothing listening at the address:port you tried to connect to. 您尝试连接的address:port上没有任何监听。
  2. There is a firewall rule in the way. 有一个防火墙规则。

More likely 1. Firewalls usually just drop the packets, which causes a connection timeout rather than a refusal. 可能性更高1.防火墙通常只是丢弃数据包,这会导致连接超时而不是拒绝。

Are you sure you can establish connection between those systems? 您确定可以在这些系统之间建立连接吗? I have compiled and run your code on Windows 7 and Linux Mint on Virtualbox and it works correctly. 我已经在Windows 7和Virtualbox的Linux Mint上编译并运行了您的代码,它可以正常运行。

What do you mean "It doesn't work"? 您是什么意思“它不起作用”? Does it throw any exception? 是否抛出任何异常? If you just don't have any output, try to run it again and wait about 30 seconds. 如果您没有任何输出,请尝试再次运行它,然后等待大约30秒钟。

For me it's just a network problem. 对我来说,这只是一个网络问题。 So you should also try to ping your windows machine from linux and then try to telnet to server. 因此,您还应该尝试从Linux ping您的Windows计算机,然后尝试远程登录到服务器。

Edit: 编辑:

So we know it is a network problem. 因此,我们知道这是网络问题。 First try to ping ip server from Linux system. 首先尝试从Linux系统ping IP服务器。

ping 192.168.224.78

If it didn't work, you should check if both machines are in the same subnet 192.168.224.0 assuming the mask is 255.255.255.0 . 如果不起作用,则应假设掩码为255.255.255.0 ,以检查两台计算机是否在同一子网192.168.224.0 You need just to type ifconfig in console. 您只需要在控制台中键入ifconfig

In next step you should try to disable windows firewall. 在下一步中,您应该尝试禁用Windows防火墙。 Here is an instruction how to do that. 这是一个说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM