C套接字服务器,Java套接字客户端:阻塞!

时间:2010-10-27 17:41:39

标签: java c

我在C和Java客户端中粘贴了一个简单套接字服务器的代码。

我使用write方法在Java中逐字符发送。然而,在发送了一大块字符(这里是'h','e','y')之后,Java客户端被阻止,因为C服务器没有回复它:(

我假设发送一个空字符(来自Java写入)会阻止C端的recv存在一些问题。

非常感谢任何帮助。

C服务器:

#include <stdio.h>   /* standard in and output*/
#include <sys/socket.h> /* for socket() and socket functions*/
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>    
#include <string.h>     
#include <unistd.h>     /* for close() */


int main(int argc, char *argv[]){
 int sock, connected, bytes_received, true = 1;  
    char recv_data;      
 char replyBuffer[32];

    struct sockaddr_in server_addr,client_addr;    
    int sin_size;       
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Socket");
        exit(1);
    }

    if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
        perror("Setsockopt");
        exit(1);
    }

    server_addr.sin_family = AF_INET;         
    server_addr.sin_port = htons(2400);     
    server_addr.sin_addr.s_addr = INADDR_ANY; 
    bzero(&(server_addr.sin_zero),8); 

    if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("Unable to bind");
            exit(1);
    }

    if (listen(sock, 5) == -1) {
        perror("Listen");
        exit(1);
    }

 printf("\nTCPServer Waiting for client on port 2400");

   while(1){  
        sin_size = sizeof(client_addr);
        connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
        printf("\n Got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));


       while ((bytes_received = recv(connected,&recv_data,1,0)) > 0){
  printf("\nrecv= %c\n", recv_data);
  }
  int success = 1;
  sprintf(replyBuffer, "%d", success); 
  printf("reply buffer = %s\n", replyBuffer);
  if (send(connected, replyBuffer, strlen(replyBuffer), 0) == -1)
    perror("send() failed"); 
    success = 0;
  close(connected);
 }
}

Java客户端:

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

public class Client1
{
 public static void main(String[] args) throws IOException {
  if (args.length < 2) {
   System.err.println("Usage: java Client1 <IP address> <Port number>");
   System.exit(0);
  }
  BufferedReader in = null;
  OutputStream out = null;
  Socket sock = null;

  try {
   sock = new Socket(args[0], Integer.parseInt(args[1]));
   out = sock.getOutputStream();
   in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

    String line = "hey";
    String responseline = null;
char[] strArray;
strArray = line.toCharArray();

while (true) {
for( int index = 0; index < strArray.length; index++){
 out.write(strArray[index]);
}
out.flush();
System.out.println("data sent " );
System.out.println("val returned"+in.readLine());
   }
  }
  catch (IOException ioe) {
   System.err.println(ioe);
  }
  finally {
   if (in != null)
    in.close();

   if (out != null)
    out.close();
   if (sock != null)
    sock.close();
  }
 }
}

4 个答案:

答案 0 :(得分:8)

来自javadoc for BufferedReader.readLine()

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. 

这就是为什么它会阻止(没有lf,没有cr,没有crlf)

[编辑]
您需要创建一个C程序可以理解的EOL指示器(就像java客户端中的readLine()一样。让指示符为'\ n'(为了保持一致性) 在Client.java中,将“\ n”附加到您发送的字符串 在tst.c中,测试'\ n',当它被接收时,从recv()循环中断 [/编辑]

答案 1 :(得分:2)

您对问题的假设是正确的。 recv调用将始终返回大于零的值。我建议使用一个特殊字符来指定文本的结尾,并在读取该字符时终止recv()循环。

答案 2 :(得分:0)

在我看来,你的服务器代码有错误。如果你在linux上编译服务器代码,就会出错。 sin_size的类型应该是socklen_t。 或者您将看到错误信息“从int *到socklen_t的无效转换”

答案 3 :(得分:0)

客户端更改为

out.write(line.getBytes());

服务器更改为

char recv_data[32];
recv(connect,recv_data,32,0);