客套话不说了:socket中的send函数可以发送字符串,不能直接发送结构体,
自己理解:结构体即内存中一段连续的内存,这样send中可以发送结构体指针 上代码:
代码功能:客户端发送给服务端一个结构体,服务端返回客户端一字符串
客户端代码 client.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 4321
#define BUFFER_SIZE 1024
typedef struct bat
{
char group[20];
int num;
}my;
int main(int argc, char *argv[])
{
int sockfd,sendbytes,recvbytes;
char buf[BUFFER_SIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc < 3)
{
fprintf(stderr,"USAGE: ./client Hostname(or ip address)Textn");
exit(1);
}
//地址解析函数
if ((host = gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname");
exit(1);
}
memset(buf, 0, sizeof(buf));
sprintf(buf, "%s", argv[2]);
//创建socket
if ((sockfd= socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
//设置sockaddr_in结构体相关参数
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr = *((struct in_addr*)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
//调用connect函数主动发起对服务器的连接
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr))== -1)
{
perror("connect");
exit(1);
}
my buff;
strcpy(buff.group,"bat001");
buff.num=24;
//发消息给服务器端
if ((sendbytes = send(sockfd,&buff,sizeof(buff),0)) == -1)
{
perror("send");
exit(1);
}
memset(buf,0,sizeof(buf));
//接收来自服务器端的返回
if ((recvbytes = recv(sockfd,buf,sizeof(buf), 0)) == -1)
{
perror("recv");
exit(1);
}
printf("Received a message :%sn",buf);
close(sockfd);
exit(0);
}
服务器端代码server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#definePORT4321
#defineBUFFER_SIZE1024
#define MAX_QUE_CONN_NM 5
typedef struct bat
{
char group[20];
int num;
}my;
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes,sendbytes;
int sockfd, client_fd;
char buf[BUFFER_SIZE];
my buff;
buff.num=10;
strcpy(buff.group,"bat001");
//建立socket连接
if ((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("socket");
exit(1);
}
printf("Socket id = %dn",sockfd);
//设置sockaddr_in结构中相关参数
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(PORT);
server_sockaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_sockaddr.sin_zero), 8);
int i = 1;//允许重复使用本地址与套接字进行绑定
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i,sizeof(i));
//调用bind()函数
if (bind(sockfd, (struct sockaddr*)&server_sockaddr,
sizeof(struct sockaddr)) == -1)
{
perror("bind");
exit(1);
}
printf("Bind success!n");
//调用listen()函数,创建未处理请求的队列
if (listen(sockfd, MAX_QUE_CONN_NM) == -1)
{
perror("listen");
exit(1);
}
while(1)
{
printf("Listening....n");
//调用accept()函数,等待客户端连接
sin_size=sizeof(client_sockaddr);
if ((client_fd = accept(sockfd,(struct sockaddr*)&client_sockaddr, &sin_size)) ==-1)
{
perror("accept");
exit(1);
}
my buff;
//调用recv()函数接收客户端请求
memset(buf , 0, sizeof(buf));
if ((recvbytes = recv(client_fd,&buff,sizeof(buff),0)) == -1)
{
perror("recv");
exit(1);
}
printf("Received a message: group=%s num=%dn",buff.group,buff.num);
memset(buf, 0, sizeof(buf));
strcpy(buf,"return to client");
if ((sendbytes = send(client_fd,buf, strlen(buf),0)) == -1)
{
perror("send");
exit(1);
}
printf("send message :%sn",buf);
}
close(sockfd);
exit(0);
}
gcc client.c -o client
gcc server.c -o server
开两个终端server终端:./server
Socket id = 3
Bind success!
Listening....
Received a message: group=bat001 num=24
send message :return to client
Listening....
client终端:./client localhost hello,server
Received a message : return to client