首页 > 解决方案 > 通过 java 套接字与 c 服务器的套接字

问题描述

我的任务要求我用 c 编写服务器,但用 java 编写客户端。我需要向我的客户发送一些整数作为指示。他们可以顺利连接,但我的 java 客户端无法接收从 c 服务器发送的整数。只有两种可能性:我的 c 服务器没有发送数字或者我的客户端没有正确接收整数。c 服务器能够在我输入时循环,因为 printf 和 scanf 被执行,而客户端没有任何事情发生。

我被困在这里,任何帮助将不胜感激!

==================================================== ======================= 更新:我更正了java中的主类,其中客户端的类名改为dotClient,我的客户端能够连接和从服务器读取输入。我尝试直接在服务器端发送一个'int',但是当客户端(java)使用DataInputStream.ReadInt()时,它返回一个随机的大数字,就好像c中int的大小和java中int的大小是不匹配。当我使用 ac 客户端做同样的工作时,它工作正常。因此,直接将 dataInputStream 与 ac 服务器一起使用存在隐藏问题,因为我也尝试了 readShort() 和 ReadLong()。

按照建议,我使用 bufferReader。并在服务器端发送字符串,并将其保存到客户端的 int 中。有用。

这是我更新的c代码

#define PORT 55555 

int main(int argc, char const *argv[]) 

{ 
int server_fd, new_socket, valread; 
struct sockaddr_in address; 
int opt = 1; 
int addrlen = sizeof(address); 

char buffer[1024]; 
int returnSend = 0;

// Creating socket file descriptor 
if (
    (server_fd = socket(AF_INET, SOCK_STREAM, 0))
     == 0) 
{ 
    perror("socket failed"); 
    exit(EXIT_FAILURE); 
} 
printf("%s\n", "Socket created!");

// Forcefully attaching socket to the port 8080 
if (setsockopt(server_fd, SOL_SOCKET,
    SO_REUSEPORT, &opt, sizeof(opt))) 
{ 
    perror("setsockopt"); 
    exit(EXIT_FAILURE); 
} 
printf("Socket attached!\n");

address.sin_family = AF_INET; 
address.sin_addr.s_addr = INADDR_ANY; 
address.sin_port = htons( PORT ); 

// Forcefully attaching socket to the port 8080 
if (bind(server_fd, 
    (struct sockaddr *)&address,  
     sizeof(address))<0) 
{ 
    perror("bind failed"); 
    exit(EXIT_FAILURE); 
} 
printf("socket binded!\n");

if (listen(server_fd, 3) < 0) 
{ 
    perror("listen"); 
    exit(EXIT_FAILURE); 
} 
printf("socket listened!\n");

if ((new_socket = accept(server_fd,
    (struct sockaddr *)&address,  
    (socklen_t*)&addrlen))<0) 
{ 
    perror("accept"); 
    exit(EXIT_FAILURE); 
}// Socket formulated !

do{
    fgets(buffer, sizeof(buffer), stdin); 
    returnSend = write(new_socket , buffer , strlen(buffer));
    printf("Sending: %sReturn returnSend: %d.\n", buffer,returnSend); 

} while (1);
return 0; 

} 

这是我更新的 Java 客户端

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

public class dotClient 
{ 
    // initialize socket and input output streams

        private Socket echoSocket= null;
        private BufferedReader input = null;

// constructor to put ip address and port 
    public dotClient(String address, int port) 
    { 
    // establish a connection 
    try
    { 
            echoSocket = new Socket(address, port); 
            System.out.println("Connected");

         input =
        new BufferedReader(
        new InputStreamReader(echoSocket.getInputStream()));

    } 
    catch(UnknownHostException u) 
    { 
        System.out.println("exception 1: "+u); 
    } 
    catch(IOException i) 
    { 
        System.out.println("exception 2: "+i); 
    } 

    int number = 0;
    String line = "";
    // keep reading until read neagaive integer

    try
    {   
        while ((line = input.readLine()) != null) 
        {
        number = Integer.parseInt(line);
        System.out.println("String is :"+line);
        System.out.println("number is :"+number);

        } 
    }
    catch(IOException i)
    { 
        System.out.println("Exception 3: "+i); 
    } 

    // close the connection 
    try
    { 
        input.close(); 
        out.close(); 
        echoSocket.close(); 
        System.out.println("Connection closed!");
    } 
    catch(IOException i) 
    { 
        System.out.println("Exception 4: "+i); 
    } 
} 

public static void main(String args[]) 
{ 
    dotClient client = new dotClient("192.168.0.3", 55555); 
} 
} 

标签: javacsockets

解决方案


In your main method you are creating a Client instance but your class is called dotClient. You might want to ensure you are creating an instance of your class dotClient. Otherwise I agree with the previous comments and would suggest BufferedReader and BufferedWriter/PrintWriter for the IO.

Do any of your try blocks catch an exception on the client? Check the return value of send(new_socket , &number , size(number) , 0 ); on the server to make sure the data was actually sent for more info checkout http://man7.org/linux/man-pages/man2/send.2.html.


推荐阅读