Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件、结构体、数字等,是在Socket编程中经常需要用到的。由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件、结构体、数字等等。
原文地址:http://www.cnblogs.com/xudong-bupt/p/3496741.html
1.memcpy
Copy block of memory。内存块拷贝函数,该函数是标准库函数,可以进行二进制拷贝数据。
函数原型: void * memcpy ( void * destination, const void * source, size_t num );
函数说明:从source指向的地址开始拷贝num个字节到以destination开始的地址。其中destination与source指向的数据类型无关。
2.Socket传输
使用memcpy将文件、结构体、数字等,可以转换为char数组,之后进行传输,接收方在使用memcpy将char数组转换为相应的数据。
下面的程序使用Socket传输结构体数据,由客户端传输给服务器端。
传输的结构体为:
typedef struct { int ab; int num[1000000]; }Node;服务器代码:
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HELLO_WORLD_SERVER_PORT 6666
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
typedef struct
{
int ab;
int num[1000000];
}Node;
int main(int argc, char **argv)
{
// set socket's address information
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
// create a stream socket
int server_socket = socket(PF_INET, SOCK_STREAM, 0);
if (server_socket < 0)
{
printf("Create Socket Failed!\n");
exit(1);
}
//bind
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
{
printf("Server Bind Port: %d Failed!\n", HELLO_WORLD_SERVER_PORT);
exit(1);
}
// listen
if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
{
printf("Server Listen Failed!\n");
exit(1);
}
while(1)
{
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
int new_server_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
if (new_server_socket < 0)
{
printf("Server Accept Failed!\n");
break;
}
Node *myNode=(Node*)malloc(sizeof(Node));
int needRecv=sizeof(Node);
char *buffer=(char*)malloc(needRecv);
int pos=0;
int len;
while(pos < needRecv)
{
len = recv(new_server_socket, buffer+pos, BUFFER_SIZE, 0);
if (len < 0)
{
printf("Server Recieve Data Failed!\n");
break;
}
pos+=len;
}
close(new_server_socket);
memcpy(myNode,buffer,needRecv);
printf("recv over ab=%d num[0]=%d num[999999]=%d\n",myNode->ab,myNode->num[0],myNode->num[999999]);
free(buffer);
free(myNode);
}
close(server_socket);
return 0;
}
客户端代码:
#include <sys/types.h> 2 #include <sys/socket.h> // 包含套接字函数库 3 #include <stdio.h> 4 #include <netinet/in.h> // 包含AF_INET相关结构 5 #include <arpa/inet.h> // 包含AF_INET相关操作的函数 6 #include <unistd.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <sys/shm.h> 11 #include <pthread.h> 12 13 #define MYPORT 6666 14 #define BUFFER_SIZE 1024 15 16 typedef struct 17 { 18 int ab; 19 int num[1000000]; 20 }Node; 21 22 int main() 23 { 24 ///sockfd 25 int sock_cli = socket(AF_INET,SOCK_STREAM, 0); 26 27 struct sockaddr_in servaddr; 28 memset(&servaddr, 0, sizeof(servaddr)); 29 servaddr.sin_family = AF_INET; 30 servaddr.sin_port = htons(MYPORT); 31 servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 32 33 if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) 34 { 35 perror("connect"); 36 exit(1); 37 } 38 39 Node *myNode=(Node*)malloc(sizeof(Node)); 40 myNode->ab=123; 41 myNode->num[0]=110; 42 myNode->num[999999]=99; 43 44 int needSend=sizeof(Node); 45 char *buffer=(char*)malloc(needSend); 46 memcpy(buffer,myNode,needSend); 47 48 int pos=0; 49 int len=0; 50 while(pos < needSend) 51 { 52 len=send(sock_cli, buffer+pos, BUFFER_SIZE,0); 53 if(len <= 0) 54 { 55 perror("ERRPR"); 56 break; 57 } 58 pos+=len; 59 } 60 free(buffer); 61 free(myNode); 62 close(sock_cli); 63 printf("Send over!!!\n"); 64 return 0; 65 }服务器端执行输出: