0

I'm trying to create simple socket application using sockets to send stream from linux (64x ArchLinux) to server (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. Is there some special way to connect Windows-Linux ?

Server.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

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"); 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

EDIT 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)
4
  • My guess is that this is an issue of little endian vs big endian. Commented Jan 2, 2014 at 21:54
  • if this is a case, is it possible to fix it somehow? Commented Jan 2, 2014 at 22:10
  • 'System.out.print("'\n");' <-- this. Have You considered the differences in line endings? Windows uses CR LF, *nix only CR. It might be, that the "readLine" never interprets the ending correctly. Commented Jan 2, 2014 at 23:59
  • 2
    1. Define 'does not work'. 2. Get rid of the ready() loop. It is literally a waste of time. The readLine() call will block. 3. When you get an exception, print it, not some crazy text of your own devising. Please fix all that and post the actual exception here. Commented Jan 3, 2014 at 0:59

2 Answers 2

2

java.net.ConnectException: Connection refused

This has two possible meanings.

  1. There is nothing listening at the address:port you tried to connect to.
  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.

Sign up to request clarification or add additional context in comments.

Comments

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.

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.

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.

Edit:

So we know it is a network problem. First try to ping ip server from Linux system.

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. You need just to type ifconfig in console.

In next step you should try to disable windows firewall. Here is an instruction how to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.