• Java C++ Socket通讯


    import java.net.*;

    import javax.swing.plaf.SliderUI;

    /**
    * 与c语言通信(java做client,c/c++做server,传送一个结构)
    *
    */
    public class Employee {
    private byte[] buf = new byte[28]; // 为说明问题,定死大小,事件中可以灵活处理


      /**
      * 将int转为低字节在前,高字节在后的byte数组
      */
      private static byte[] tolh(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
      }
      /**
      * 将byte数组转化成String
      */
      private static String toStr(byte[] valArr,int maxLen) {
        int index = 0;
        while(index < valArr.length && index < maxLen) {
          if(valArr[index] == 0) {
            break;
          }
          index++;
        }
        byte[] temp = new byte[index];
        System.arraycopy(valArr, 0, temp, 0, index);
        return new String(temp);
      }
      /**
      * 将低字节在前转为int,高字节在后的byte数组
      */
      private static int vtolh(byte[] bArr) {
        int n = 0;
        for(int i=0;i<bArr.length&&i<4;i++){
          int left = i*8;
          n+= (bArr[i] << left);
        }
        return n;
      }
      public String name = "";
      public int id = 0;
      public float salary = 0;
      /**
      * 将float转为低字节在前,高字节在后的byte数组
      */
      private static byte[] tolh(float f) {
        return tolh(Float.floatToRawIntBits(f));
      }
      private static Employee getEmployee(byte[] bArr) {
        String name = "";
        int id = 0;
        float salary = 0;
        byte[] temp = new byte[20];
        name = toStr(bArr,20);
        System.arraycopy(bArr, 20, temp, 0, 4);
        id = vtolh(temp);
        return new Employee(name, id, salary);
      }
      /**
      * 构造并转换
      */
      public Employee(String name, int id, float salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
        byte[] temp = name.getBytes();
        System.arraycopy(temp, 0, buf, 0, temp.length);
        temp = tolh(id);
        System.arraycopy(temp, 0, buf, 20, temp.length);
        temp = tolh(salary);
        System.arraycopy(temp, 0, buf, 24, temp.length);
      }
      /**
      * 返回要发送的数组
      */
      public byte[] getbuf() {
        return buf;
      }
      /**
      * 发送测试
      */
      public static void main(String[] args) {
        try {
          int index = 0;
          byte[] receive = new byte[28];
          while(true){
            Socket sock = new Socket("localhost", 5050);
            System.out.println("send content: name=kingfish "+"value="+(1+index));
            sock.getOutputStream().write(new Employee("kingfish"+index, 1+index++, 10.99f + index).getbuf());
            sock.getInputStream().read(receive);
            Employee ee = getEmployee(receive);
            printHexString(receive);
            System.out.println(" response: name="+ee.name+" "+"value="+ee.id);
            sock.close();
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      /**
      *
      *输出十六进制
      */
      private static void printHexString(byte[] b){
        String sd ="";
        for(int i=0;i<b.length;i++){
          String hex = Integer.toHexString(b[i] & 0xFF);
          if(hex.length() == 1){
            hex = '0' + hex;
          }
          sd = sd + hex.toUpperCase();
        }
        char[] strChar = sd.toCharArray();
        for(char aa:strChar){
          System.out.print(aa);
        }
        System.out.println("");
      }
    }

    ----------------------------------------------------------------------------------------

    #include<iostream>
    #include<stdlib.h>
    #include<string.h>

    #include <stdio.h>
    #include <WINSOCK2.H>

    using namespace std;

    struct UserInfo {
      int age;
      char UserName[20];
      int UserId;
    };
    struct Employee {
      UserInfo user;
      float salary;
    };

    namespace EP
    {
      void func()
      {
        WSADATA wsaData;
        char buf[1024];
        int nBytes = 1024, recvbytes;
        SOCKET Listening;
        SOCKET NewConnection;
        SOCKADDR_IN ServerAddr;
        SOCKADDR_IN ClientAddr;
        int ClientAddrLen = sizeof(ClientAddr);
        int Port = 5050;
        //int c;
        WSAStartup(MAKEWORD(2, 2), &wsaData);
        Listening = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        ServerAddr.sin_family = AF_INET;
        ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        ServerAddr.sin_port = htons(Port);

        bind(Listening, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));
        listen(Listening, 5);
        printf("Wating accpet.... ");


        while (true) {
          NewConnection = accept(Listening, (SOCKADDR *)&ClientAddr, &ClientAddrLen);
          printf("Wating recv..... ");
          if ((recvbytes = recv(NewConnection, buf, nBytes, 0)) == SOCKET_ERROR)
          {
            printf(" Errorcode=%d, Recv from client ", WSAGetLastError());
            //return 0;
          }
          Employee *data = new Employee;
          data = (Employee *)&buf;

          printf("Userid: %d Username: %s Salary: %f", data->user.UserId, data->user.UserName, data->salary);
          data->user.UserId = 006;
          //data->user.age = 23;
          strcpy_s(data->user.UserName, "Test");
          data->salary = 800;

          send(NewConnection, buf, recvbytes, 0);

        }
        //return 0;
      }
    }
    #pragma warning(disable:4996)

    int main(void)
    {
      EP::func();

      system("pause");

      return 0;
    }

    转自:http://blog.csdn.net/nupt123456789/article/details/8047619

  • 相关阅读:
    LUOGU P1654 OSU! (概率期望)
    poj 3682 King Arthur's Birthday Celebration (期望dp)
    CF148D Bag of mice (期望dp)
    LUOGU P1514 引水入城 (bfs)
    LUOGU P4281 [AHOI2008]紧急集合 / 聚会 (lca)
    LUOGU P1313 计算系数 (组合数学)
    LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)
    LUOGU P1613 跑路 (倍增floyd)
    LUOGU P1291 [SHOI2002]百事世界杯之旅 (期望dp)
    poj 3208--Apocalypse Someday(数位dp)
  • 原文地址:https://www.cnblogs.com/herd/p/7006937.html
Copyright © 2020-2023  润新知