简体   繁体   中英

can't connect Java client to C server

I have a very simple server written in C and an equally simple client written in Java. When I run them both on the same computer everything works, but when I try to run the server on computer A and the client on computer B, I get the error IOException connection refused from the java client. I can't seem to find out whats happening, any thoughts? I've even turned off the firewalls but the problem still persists.

server.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 3557
#define BUF 256

int main(int argc, char *argv[])
{
    struct sockaddr_in host, remote;
    int host_fd, remote_fd;
    int size = sizeof(struct sockaddr);;

    char data[BUF];

    host.sin_family = AF_INET;
    host.sin_addr.s_addr = htonl(INADDR_ANY);
    host.sin_port = htons(PORT);
    memset(&host.sin_zero, 0, sizeof(host.sin_zero));

    host_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(host_fd == -1) {
        printf("socket error %d\n", host_fd);
        return 1;
    }

    if(bind(host_fd, (struct sockaddr *)&host, size)) {
        printf("bind error\n");
        return 1;
    }

    if(listen(host_fd, 5)) {
        printf("listen error");
        return 1;
    }

    printf("Server setup, waiting for connection...\n");
    remote_fd = accept(host_fd, (struct sockaddr *)&remote, &size);

    printf("connection made\n");

    int read = recv(remote_fd, data, BUF, 0);
    data[read] = '\0';
    printf("read = %d, data = %s\n", read, data);

    shutdown(remote_fd, SHUT_RDWR);
    close(remote_fd);

    return 0;
}

client.

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

public class socket {

    public static void main(String[] argv) {
        DataOutputStream os = null;

        try {
            Socket socket = new Socket("192.168.1.103", 3557);
            os = new DataOutputStream(socket.getOutputStream());
            os.writeBytes("phone 12");

            os.close();
            socket.close();

        } catch (UnknownHostException e) {
            System.out.println("Unkonw exception " + e.getMessage());

        } catch (IOException e) {
            System.out.println("IOException caught " + e.getMessage());
        } 
    }
}

Edit:

Thanks everyone for the quick responses (I was shocked). I solved the problem thanks to the suggestions of using telnet and nc. I found out that my firewall was blocking port 3557 on the server machine. A simple thing I should have thought of, thanks!

There are many possible problems and you should start and try pinpointing the problematic part.

It just might be a networking problem - B might not be able to connect to A because of firewalls and the likes.

To check this you can start with running a port scan from B on A ( nmap -p 3557 192.168.1.103 to check if the port seems open to computer B).

If it looks open, you can try connecting with telnet/nc from B instead of your client to see if the server works as expected: telnet 192.168.1.103 3557 or nc 192.168.1.103 3557 and once connected write "phone 12" and see what you get.

These should help you understand which part is causing problems, server, client or network.

Well, the first thing to do is believe the error message: if you're getting "Connection Refused" assume the other end is refusing the connection. The question then is Why?

Almost always, it turns out that it's a simple "dumb mistake" sort of thing. Before you do anything deep, check that:

  • The IP addresses and ports are correct
  • there are no spare processes also trying to use port 3557 (although that would normally give a different error message)
  • that your server side runs and stays up in I/O Wait state before you try to connect
  • that the two ends work when run on the on the same machine.

Assuming all of those work, then use telnet(1) to try and connect to the server. If it's going to work, telnet should be able to read the other end.

The commend actually takes two arguments, host and port:

$ telnet 192.168.1.103 3557

should connect to the server on either machine, and of course

$ telnet localhost 3557

should work on the server-side system.

If the programs work as long as you're on the same system, but not remotely, you've got some kind of routing or firewall problem. (Make sure you don't have a "smart" switch trying to restrict certain ports outside of what your firewall is doing.)

If the programs won't work running on the same system, make sure the server is waiting for I/O, and try the telnet trick. If the server is not stopped waiting for I/O, read the man page for the flags on your library calls. No one ever remembers them all; some of them have mysterious side effects you wouldn't notice.

If the server is stopped waiting for I/O and telnet won't connect, then add some printfs to make sure it's progressing as you plan. in particular, have a look at the return values of the accept and recv -- if you have a program that's not behaving, and you haven't tested all the return codes, you can bet (it's one of Murphy's Laws no doubt) that one of the calls you don't test is failing.

If you can connect via telnet and type data at it successfully, move on to the Java. Try making os a FileOutputStream and see if it does what you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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