11

我正在尝试通过套接字与 Java 客户端和 C 服务器进行通信 如果我尝试在命令行上使用 nc 连接服务器,或者如果我使用 nc 作为服务器并与我的 Java 客户端连接,那么一切似乎都可以正常工作,但是当我尝试连接Java客户端和C服务器它不起作用。

客户端启动连接并发送消息,服务器接收到消息,但服务器的响应永远不会到达客户端。

服务器代码:

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

int main( int argc, char *argv[] )
{
    int sockfd, clisockfd, portno;
    char * start = "hello";
    char * end = "bye";
    socklen_t clilen;
    char buffer[256];
    char contentBuffer[255];
    struct sockaddr_in serv_addr, cli_addr;
    int  n;
    //int optval;

    /* First call to socket() function */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
    {
        perror("ERROR opening socket");
        return(1);
    }

    /* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5000;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);


    if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    {
        perror("ERROR on binding");
        return(1);
    }

    listen(sockfd,5);
    clilen = (socklen_t) sizeof(cli_addr);

    clisockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);

    if (clisockfd < 0) 
    {
        perror("ERROR on accept");
        return(1);
    }

    while (strcmp(end, contentBuffer) !=0)
    {
        bzero(buffer,256);
        bzero(contentBuffer,255);
        /* If connection is established then start communicating */
        n = read( clisockfd,buffer,255 );
        if (n < 0)
        {
            perror("ERROR reading from socket");
            return(1);
        }

        strncpy(contentBuffer,buffer,strlen(buffer) - 1);  
        if (strcmp(start, contentBuffer) ==0)
        {
            printf("command: %s\n",buffer);
            n = write(clisockfd,"Roger that",11);
            if (n < 0)
            {
                perror("ERROR writing to socket");
                return(1);
            }
        }
        else 
        {
            printf("Unknown command: %s\n",buffer);
            n = write(clisockfd,"ERRCmd",7);
            if (n < 0)
            {
                perror("ERROR writing to socket");
                return(1);
            }           
        }
    }
    close(sockfd);
    return 0;


}

客户代码

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

public class Cliente {
    public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            System.err.println(
                "Usage: java EchoClient <host name> <port number>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);
        Socket firstSocket = new Socket(hostName, portNumber);
        PrintWriter out = new PrintWriter(firstSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(firstSocket.getInputStream()));
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String userInput;
        while ((userInput = stdIn.readLine()) != null) 
        {
            out.println(userInput);
            System.out.println("received: " + in.readLine());
        }
        in.close();
        stdIn.close();
        firstSocket.close();

    }
}
4

1 回答 1

12

readLine()阻塞,直到连接已经shutdown()close()d 或它收到一个 new-line \n,它永远不会由服务器发送。

于 2013-10-24T09:35:05.837 回答