client
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
// #include <sys/shm.h>
#define MYPORT 9988
#define BUFFER_SIZE 1024
const char* SERVER_IP = "127.0.0.1";/
int xxxx_client_socket_fd;
int xxxx_socket_send_data(char* xxxx_data, int data_length);
int main()
{
///定义sockfd
xxxx_client_socket_fd = socket(AF_INET,SOCK_STREAM, 0);
///定义sockaddr_in
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MYPORT); ///服务器端口
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP); ///服务器ip
int len;
int i ;
printf("连接%s:%d\n",SERVER_IP,MYPORT);
///连接服务器,成功返回0,错误返回-1
if (connect(xxxx_client_socket_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("connect");
exit(1);
}
printf("服务器连接成功\n");
//char sendbuf[BUFFER_SIZE];
char recvbuf[BUFFER_SIZE];
const char *sendbuf = "carl_test_1_xxxx_5e";
const int LEN = 100000;
const int LENEX = 200000; //缓冲区200KB
int recvLen = 0;
char temp[LEN];
char buf[LENEX];
int _res ; // = recv(_sock, temp, LEN, 0);
int char_flag = 1; // first
int char_falg = 2; // second
memset(buf,0,LENEX);
memset(temp,0,LEN);
while (1) {
send(xxxx_client_socket_fd, sendbuf, strlen(sendbuf),0); ///发送
printf(" socket send %s \n ",sendbuf);
_res = recv(xxxx_client_socket_fd, temp, LEN, 0);
if (_res < 0 )
{
printf(" socket error \n ");
break;
}
if (_res > 512 )
{
printf(" socket error \n ");
break;
}
if (!_res) /// -- 1
{
printf(" the connection has been gracefully closed. \n ");
break;
}else {
for (i=0; i< _res; i++ ){ //_res 一次 recv 数字 //5e te
buf[recvLen] = temp[i];
printf("buf 111111 %c \n",buf[recvLen]);
#if 1
if ( buf[recvLen] == 'e' ){
if ( buf[recvLen-1] == '5' ){
printf("buf 2222222 end %c %c \n",buf[recvLen-1],buf[recvLen]);
printf("buf 33333 end %s \n",buf);
memset(buf,0,LENEX);
goto end;
// printf("buf 111111 %c %c \n",buf[recvLen-1],buf[recvLen]);
}else{
}
}
#endif
recvLen++;
//memset(temp,0,LEN);
}
}
}
end:
close(xxxx_client_socket_fd);
return 0;
}
int xxxx_socket_send_data(char* xxxx_data, int data_length){
int nwrite;
char infoReq[2048];
char cntBuf[16];
nwrite = send(xxxx_client_socket_fd, xxxx_data, data_length, 0);
if ( nwrite > 0 )
{
// printf("xxxx_socket_send_data OK %d \n",nwrite);
}
else
{
// printf("xxxx_socket_send_data faield %d \n",nwrite);
}
return nwrite;
}
server
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
// #include <sys/shm.h>
#define MYPORT 9988
#define QUEUE 20
#define BUFFER_SIZE 1024
unsigned char Test_array_one[8]=
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
int xxxx_client_socket_fd ;
int conn;
int xxxx_socket_send_data(char* xxxx_data, int data_length);
int xxxx_socket_recv_data(char* xxxx_data);
int main()
{
///定义sockfd
xxxx_client_socket_fd = socket(AF_INET,SOCK_STREAM, 0);
///定义sockaddr_in
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(MYPORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
///bind,成功返回0,出错返回-1
if(bind(xxxx_client_socket_fd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf(" coonec port %d \n",MYPORT);
///listen,成功返回0,出错返回-1
if(listen(xxxx_client_socket_fd,QUEUE) == -1)
{
perror("listen");
exit(1);
}
///客户端套接字
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
printf("wait connect ..... \n");
///成功返回非负描述字,出错返回-1
conn= accept(xxxx_client_socket_fd, (struct sockaddr*)&client_addr, &length);
if(conn<0)
{
perror("connect");
exit(1);
}
printf("client conn successful \n");
// char *buffer = "carl_test_test";
memset(buffer,0,BUFFER_SIZE);
while(1)
{
int len = xxxx_socket_recv_data(buffer); //recve client
printf("server recv data %s \n",buffer); //print
xxxx_socket_send_data(buffer,strlen(buffer)); // send to client
printf("servier send %s >>> \n",buffer);
memset(buffer,0,BUFFER_SIZE);
//sleep(5);
}
close(conn);
close(xxxx_client_socket_fd);
return 0;
}
int xxxx_socket_send_data(char* xxxx_data, int data_length){
int nwrite;
char infoReq[2048];
char cntBuf[16];
nwrite = send(conn, xxxx_data, data_length, 0);
printf(">>>> server %s \n ",xxxx_data);
if ( nwrite > 0 )
{
// printf("xxxx_socket_send_data OK %d \n",nwrite);
}
else
{
// printf("xxxx_socket_send_data faield %d \n",nwrite);
}
return nwrite;
}
int xxxx_socket_recv_data(char* xxxx_data){
int nwrite;
char infoReq[2048];
char cntBuf[16];
nwrite = recv(conn, xxxx_data, sizeof(xxxx_data), 0);
if ( nwrite > 0 )
{
// printf("xxxx_socket_send_data OK %d \n",nwrite);
}
else
{
// printf("xxxx_socket_send_data faield %d \n",nwrite);
}
return nwrite;
}