gswkzj1234 2015-04-27 02:00 采纳率: 0%
浏览 3227

关于java与c的socket通信问题

服务器端是java,客户端是c,通过socket,tcp通信,客户端将一张图片传输到服务器,服务器接收完成后返回一个‘ok’字符串作为应答。运行的时候,服务器可以接收到客户端发来的图片,但是客户端无法接收到服务器的‘ok’应答。对socket了解不深,求大神指点,谢谢了~代码如下:

server(java):

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

public class ServerTcp {
public static void main(String args[])throws Exception{

ServerSocket server = new ServerSocket(8866);
byte[] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
PrintStream out = null;
fos = new FileOutputStream(new File("/home/gsw/test/cc.jpg"));
inputByte = new byte[5120];

System.out.println("等待链接...");
Socket client = server.accept();

dis = new DataInputStream(client.getInputStream());
out = new PrintStream(client.getOutputStream());

while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
    System.out.println(length);
    fos.write(inputByte, 0, length);
    fos.flush();
}

System.out.println("完成接收");

out.print("ok");                        //服务器应答

dis.close();
out.close();
fos.close();
client.close();
server.close();
System.out.println("关闭!");
}

}

client(c):

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SER_PORT 8866

#define MAX_LEN 256

int main(int argc,char **argv)

{

    int     sd,rn; 

    struct sockaddr_in   client,server; 

    char    send_buf[MAX_LEN]; 
    char    recv_buf[MAX_LEN]; 

    FILE    *fq; 

    int     len,opt = 1; 

    if( ( sd = socket(AF_INET,SOCK_STREAM,0)) == -1 )

    { 

            perror("socket"); 

            exit(1); 

    } 

    bzero(&server,sizeof(server));

    server.sin_family = AF_INET;

    server.sin_port = htons(SER_PORT);

    server.sin_addr.s_addr = inet_addr("127.0.0.1");

     setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); 

     if( connect(sd,(struct sockaddr *)&server,sizeof(server)) < 0 ) 

    { 

            perror("Connect");

            close(sd);

            exit(1); 

    } 

    if( ( fq = fopen("1.jpg","rb") ) == NULL )

    { 

            perror("File open");

            close(sd);

            exit(1); 

    }

    bzero(send_buf,sizeof(send_buf)); 
    memset(send_buf, 0, MAX_LEN);

    while( !feof(fq) )

    {

            len = fread(send_buf,1,MAX_LEN,fq);
            if( len != write(sd,send_buf,len) )

            {

                    perror("write");

                    break;

           }

    }
   memset(recv_buf, 0, MAX_LEN);
   printf("send all!\n");

     if(read(sd,recv_buf,sizeof(recv_buf)) ==-1)    //接收应答
    {
        perror("read");
        exit(1);
    }

    printf("received from server:%s\n",recv_buf);

    close(sd); 

    fclose(fq); 

    return 0; 

}

程序也不报错,被阻塞了
server端控制台
图片说明
client端
图片说明

  • 写回答

2条回答 默认 最新

  • bingcai126com 2015-04-27 05:09
    关注

    试试
    out.println("ok");

    out.print("ok");
    out.flush();

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog