博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++做client Java做客户端传送数据
阅读量:5932 次
发布时间:2019-06-19

本文共 3880 字,大约阅读时间需要 12 分钟。

因为要用到,但发现Java怎么都收不到C发来的数据,除非C端自动挂掉,java会一口气全收回来。

后来才发现是因为C发过来的Java用readline是读不到回车的,所以会一直等待。

所以不要用readline(),这是不正确滴。

具体应该如下:

Java Server端:

1 package jsi; 2  3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 10 public class MyServer {11 12     public ServerSocket server;13             14     public MyServer(){15         new Thread(new Runnable(){16             private ServerSocket server;17 18             @Override19             public void run(){20                 try {21                     server = new ServerSocket(31003);22                     Socket socket = server.accept();23                     BufferedReader read = new BufferedReader(new 24                             InputStreamReader(socket.getInputStream()));25                     PrintWriter print = new PrintWriter(socket.getOutputStream());26                     while(true){27                         char[] cbuf = new char[70];28                         read.read(cbuf);   // don't use : String msg = read.readline();29                         String msg = new String(cbuf);30                         if(msg.equals("bye")){31                             break;32                         }33                         print.write("OK");34                         print.flush();35                         double mm = Double.parseDouble(msg);36                         System.out.println(mm);37                         38                     }39                 } catch (IOException e) {40                     // TODO Auto-generated catch block41                     e.printStackTrace();42                 }43             }44             45         }).start();46     }47     /**48      * @param args49      */50     public static void main(String[] args) {51         // TODO Auto-generated method stub52         new MyServer();53     }54 55 }

C++客户端:

/* * Client.cpp * *  Created on: 2013-11-29 *      Author: root */#include 
#include
// for sockaddr_in#include
// for socket#include
// for socket#include
// for printf#include
// for exit#include
// for bzero#include
#include
#include
#define SERVER_PORT 31003#define MAXDATA 50int main(int argc, char **argv){ struct sockaddr_in client_addr; bzero(&client_addr, sizeof(client_addr)); //set zero of this memory client_addr.sin_family = AF_INET; //internet protocol client_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY means get the local address client_addr.sin_port = htons(0); //0 means get a free port int client_socket = socket(AF_INET, SOCK_STREAM, 0); if (client_socket < 0) { printf("Create Socket Failed!\n"); exit(1); } //bind the socket and ip address of the client machine if (bind(client_socket, (struct sockaddr*) &client_addr, sizeof(client_addr))) { printf("Client Bind Port Failed!\n"); exit(1); } //socket struct had the server_addr and server port struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; if (inet_aton("127.0.0.1", &server_addr.sin_addr) == 0) { printf("Server IP Address Error! \n"); exit(1); } server_addr.sin_port = htons(SERVER_PORT); socklen_t server_addr_length = sizeof(server_addr); if (connect(client_socket, (struct sockaddr*) &server_addr, server_addr_length) < 0) { printf("Can Not Connect To localhost!\n"); exit(1); }else printf("connect successful! \n"); std::string buffer; char buf[MAXDATA]; bool flag = false; while(1){ buffer = flag == true ? "13" : "10";     if(write(client_socket,buffer.c_str(),buffer.length()) == -1) { perror("send error!"); exit(1); } sleep(1); flag = !flag; if((recv(client_socket, buf, MAXDATA,0)) == -1) { perror("receive error!"); exit(1); } printf("Received: %s\n",buf); }}

 

 

转载地址:http://ccktx.baihongyu.com/

你可能感兴趣的文章
IntersectionObserver + Custom Elements 实现图片懒加载(滚动加载)/点击重试
查看>>
KSImageNamed-Xcode-master
查看>>
memcache
查看>>
Struts2参数知识点
查看>>
tomcat 8.0虚拟机配置文档
查看>>
轻松实现基于Heartbeat的高可用web服务集群
查看>>
分析y一款APP
查看>>
我的 ubuntu14.04.3 LTS
查看>>
mac 安装office
查看>>
学习bufio、fmt
查看>>
nginx 设置301重定向
查看>>
mycat 数据迁移测试
查看>>
SSDCRM用户体验版定期发布地址公布
查看>>
记录下测试服务器频繁死机问题解决
查看>>
489 - Hangman Judge
查看>>
Python3 列表
查看>>
基于Btrace的监控调试
查看>>
Spring船新版推出的WebFlux,是兄弟就来学我
查看>>
Python编程语言历史及特性01
查看>>
ListView常用的属性
查看>>