一、服务端 c++
[email protected]:~/Public/xtranfer$ cat ./main.c #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <arpa/inet.h> #include <string.h> #include "cJSON/cJSON.h" #define BUFFER_SIZE 255 #define SERVER_PORT 13579 #define LIGEN_OF_LISTEN_QUEUE 5 char* doit(); int main(int args, char** argv) { const char* welcome = "welcome to shaoyongyang‘s server\n"; const char* welcome_tip = "please input your id:\n"; //init socket int servfd,clifd; struct sockaddr_in server_addr,client_addr; //test json const char *json = doit(); if((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0) { printf("socket create error!\n"); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); server_addr.sin_addr.s_addr = htons(INADDR_ANY); if(bind(servfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0) { printf("bind to port %d\n failure!", SERVER_PORT); exit(1); } if(listen(servfd, LIGEN_OF_LISTEN_QUEUE) < 0) { printf("create listen failure!\n"); exit(1); } while(1) { char buf[BUFFER_SIZE]; long timestamp; socklen_t length = sizeof(client_addr); clifd = accept(servfd,(struct sockaddr*)&client_addr,&length); if(clifd < 0) { printf("accept error !\n"); break; } //close and print message printf("from client IP:%s,Port:%d\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); timestamp = time(NULL); int x = strlen(json); printf("-- \n\n%s\n--", json); printf("-- send size:%d --\n", x); send(clifd,json,x,0); close(clifd); } close(servfd); doit(); } char* doit() { cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; root=cJSON_CreateObject(); cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject()); cJSON_AddStringToObject(fmt,"type", "rect"); cJSON_AddNumberToObject(fmt,"width", 1920); cJSON_AddNumberToObject(fmt,"height", 1080); cJSON_AddFalseToObject (fmt,"interlace"); cJSON_AddNumberToObject(fmt,"frame rate", 24); out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); return out; } [email protected]:~/Public/xtranfer$
我引用的是cJSON
项目地址:https://github.com/kbranigan/cJSON
编译的时候需要执行
g++ cJSON/cJSON.c main.c -o main -lm
二、客户端 java
package com.divego.web.controller; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * Created by shaoyongyang on 2015/5/25. */ public class SocketController { public static void main(String args[]) { try { Socket socket = new Socket("192.168.1.149", 13579); //向本机的4700端口发出客户请求 BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); //由系统标准输入设备构造BufferedReader对象 PrintWriter os = new PrintWriter(socket.getOutputStream()); //由Socket对象得到输出流,并构造PrintWriter对象 BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); //由Socket对象得到输入流,并构造相应的BufferedReader对象 // System.out.println("i am recv the length:"+ is.readLine().); String info = null; while((info=is.readLine()) != null) { System.out.println(info); } //从Server读入一字符串,并打印到标准输出上 os.close(); //关闭Socket输出流 is.close(); //关闭Socket输入流 socket.close(); //关闭Socket } catch (Exception e) { System.out.println("Error" + e); //出错,则打印出错信息 } } }
正如代码,客户端仅仅负责连接,读取数据。
总结:
如果我们把服务端加入,多线程,数据库操作, 缓存等等, 那么能不能理解为另一套数据层处理框架,c++的性能绝对是没法说的。all right, 继续琢磨吧。
顺带截几张图好了。
时间: 2024-12-30 00:07:12