C++做client Java做客户端传送数据

因为要用到,但发现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             @Override

19             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 block

41                     e.printStackTrace();

42                 }

43             }

44             

45         }).start();

46     }

47     /**

48      * @param args

49      */

50     public static void main(String[] args) {

51         // TODO Auto-generated method stub

52         new MyServer();

53     }

54 

55 }

C++客户端:

/*

 * Client.cpp

 *

 *  Created on: 2013-11-29

 *      Author: root

 */



#include <errno.h>

#include <netinet/in.h>    // for sockaddr_in

#include <sys/types.h>    // for socket

#include <sys/socket.h>    // for socket

#include <stdio.h>        // for printf

#include <stdlib.h>        // for exit

#include <string.h>        // for bzero

#include <arpa/inet.h>

#include <unistd.h>

#include <string>

#define SERVER_PORT 31003

#define MAXDATA 50

int 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);

    }

}

 

 

你可能感兴趣的:(client)