gswkzj1234
gswkzj1234
采纳率0%
2015-04-27 02:00 阅读 3.2k

关于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 bingcai126com 2015-04-27 05:09

    试试
    out.println("ok");

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

    点赞 评论 复制链接分享
  • wx1396530501 西瓜csdn 2016-10-19 04:12

    回复gswkzj1234: 你好,请问下问题解决了吗,我也遇到了这个问题,想请教下

    点赞 评论 复制链接分享

相关推荐