linux 下的socket 客户端开发
/*
#define EXIT -1
#define USER 1
#define MSG 2
#define OK 3
#define CMSGLEN 100
struct CLIENTMSG{
int OP;
char username[20];
char buf[CMSGLEN];
};
*/
#include"stdio.h"
#include"stdlib.h"
#include"sys/stat.h"
#include"sys/types.h"
#include"netinet/in.h"
#include"string.h"
#include"clientmsg.h"
int main()
{
int fd;
int port=123345;
char ip[20]="127.0.0.1";
char globalname[20];
//定义结构体CLIENTMSG ,负责向服务器发送数据
struct CLIENTMSG *sendmsg;
//定义结构体CLIENTMSG ,负责接收来自服务器的数据
struct CLIENTMSG *recvmsg;
//输入服务器ip
printf("please enter the server ip:\n"); scanf("%s",&ip);
//输入服务器端口
printf("please enter the server port:\n"); scanf("%d",&port);
//定义结构体地址
struct sockaddr_in server;
server.sin_port=htons(port);
server.sin_family=inet_addr(ip);
server.sin_addr.s_addr=INADDR_ANY;
// 1. socket
if(-1== (fd=(socket(AF_INET,SOCK_STREAM,0))))
{
perror("socket error");
exit(1);
}
// 2. connect
socklen_t len = sizeof(server);
if(-1==(connect(fd,(struct sockaddr*)&server,len)))
{
perror("connect error");
exit(1);
}
struct CLIENTMSG *tempmsg;
char tempbuf[CMSGLEN];
tempmsg=(struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
if(-1==(read(fd,tempbuf,sizeof(tempbuf))))
{
perror("read error");
exit(1);
}
memcpy(tempmsg,tempbuf,sizeof(tempbuf));
if(tempmsg->OP==EXIT)
{
printf("server is refuse,maybe the client is full\n");
exit(1);
}
else if(tempmsg->OP==OK)
{
printf("%s\n",tempmsg->buf);
memset(tempmsg,0,sizeof(tempmsg));
scanf("%s",tempmsg->username);
strcpy(globalname,tempmsg->username);
tempmsg->OP=USER;
strcpy(tempmsg->buf,"NULL");
memcpy(tempbuf,tempmsg,sizeof(tempmsg));
write(fd,tempbuf,sizeof(tempbuf));
}
// 4. 创建子进程 ,负责接收数据
pid_t pid =fork();
// 5. 如果pid>0 ,说明在父进程,父进程负责向服务器发送数据
if(pid>0)
{
char sendbuf[CMSGLEN];
//5-1. 为结构体申请内存,避免内存溢出
sendmsg = (struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
//5-2. while 循环向服务器发送数据
while(1)
{
memset(sendmsg,0,sizeof(sendmsg));
bzero(sendbuf,sizeof(sendbuf));
strcpy(sendmsg->username,globalname);
printf("please input your message:\n");scanf("%s",sendmsg->buf);
sendmsg->OP=MSG;
memcpy(sendbuf,sendmsg,sizeof(sendmsg));
write(fd,sendbuf,sizeof(sendbuf));
if(!strcpy(sendbuf,"bye"))
{
break;
}
}
wait();
close(fd);
}
// 6. 如果pid==0,说明在子进程,子进程负责接收服务器传来的的数据
if(pid==0)
{
char recvbuf[CMSGLEN];
//6-1. 为结构体申请内存,避免内存溢出
recvmsg = (struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
//6-2. while 循环接收来自服务器的数据
while(1)
{
//6-3. 将结构体清空
memset(recvmsg,0,sizeof(recvmsg));
bzero(recvbuf,sizeof(recvbuf));
if(-1==(read(fd,recvbuf,sizeof(recvbuf))))
{
perror("read error");
}
memcpy(recvmsg,recvbuf,sizeof(recvbuf));
if(recvmsg->OP==EXIT)
{
printf("%s\n",recvmsg->buf);
break;
}
printf("%s\n",recvmsg->buf);
}
}
// while 退出,关闭socket
return 0;
}
#define EXIT -1
#define USER 1
#define MSG 2
#define OK 3
#define CMSGLEN 100
struct CLIENTMSG{
int OP;
char username[20];
char buf[CMSGLEN];
};
*/
#include"stdio.h"
#include"stdlib.h"
#include"sys/stat.h"
#include"sys/types.h"
#include"netinet/in.h"
#include"string.h"
#include"clientmsg.h"
int main()
{
int fd;
int port=123345;
char ip[20]="127.0.0.1";
char globalname[20];
//定义结构体CLIENTMSG ,负责向服务器发送数据
struct CLIENTMSG *sendmsg;
//定义结构体CLIENTMSG ,负责接收来自服务器的数据
struct CLIENTMSG *recvmsg;
//输入服务器ip
printf("please enter the server ip:\n"); scanf("%s",&ip);
//输入服务器端口
printf("please enter the server port:\n"); scanf("%d",&port);
//定义结构体地址
struct sockaddr_in server;
server.sin_port=htons(port);
server.sin_family=inet_addr(ip);
server.sin_addr.s_addr=INADDR_ANY;
// 1. socket
if(-1== (fd=(socket(AF_INET,SOCK_STREAM,0))))
{
perror("socket error");
exit(1);
}
// 2. connect
socklen_t len = sizeof(server);
if(-1==(connect(fd,(struct sockaddr*)&server,len)))
{
perror("connect error");
exit(1);
}
struct CLIENTMSG *tempmsg;
char tempbuf[CMSGLEN];
tempmsg=(struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
if(-1==(read(fd,tempbuf,sizeof(tempbuf))))
{
perror("read error");
exit(1);
}
memcpy(tempmsg,tempbuf,sizeof(tempbuf));
if(tempmsg->OP==EXIT)
{
printf("server is refuse,maybe the client is full\n");
exit(1);
}
else if(tempmsg->OP==OK)
{
printf("%s\n",tempmsg->buf);
memset(tempmsg,0,sizeof(tempmsg));
scanf("%s",tempmsg->username);
strcpy(globalname,tempmsg->username);
tempmsg->OP=USER;
strcpy(tempmsg->buf,"NULL");
memcpy(tempbuf,tempmsg,sizeof(tempmsg));
write(fd,tempbuf,sizeof(tempbuf));
}
// 4. 创建子进程 ,负责接收数据
pid_t pid =fork();
// 5. 如果pid>0 ,说明在父进程,父进程负责向服务器发送数据
if(pid>0)
{
char sendbuf[CMSGLEN];
//5-1. 为结构体申请内存,避免内存溢出
sendmsg = (struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
//5-2. while 循环向服务器发送数据
while(1)
{
memset(sendmsg,0,sizeof(sendmsg));
bzero(sendbuf,sizeof(sendbuf));
strcpy(sendmsg->username,globalname);
printf("please input your message:\n");scanf("%s",sendmsg->buf);
sendmsg->OP=MSG;
memcpy(sendbuf,sendmsg,sizeof(sendmsg));
write(fd,sendbuf,sizeof(sendbuf));
if(!strcpy(sendbuf,"bye"))
{
break;
}
}
wait();
close(fd);
}
// 6. 如果pid==0,说明在子进程,子进程负责接收服务器传来的的数据
if(pid==0)
{
char recvbuf[CMSGLEN];
//6-1. 为结构体申请内存,避免内存溢出
recvmsg = (struct CLIENTMSG*)malloc(sizeof(struct CLIENTMSG));
//6-2. while 循环接收来自服务器的数据
while(1)
{
//6-3. 将结构体清空
memset(recvmsg,0,sizeof(recvmsg));
bzero(recvbuf,sizeof(recvbuf));
if(-1==(read(fd,recvbuf,sizeof(recvbuf))))
{
perror("read error");
}
memcpy(recvmsg,recvbuf,sizeof(recvbuf));
if(recvmsg->OP==EXIT)
{
printf("%s\n",recvmsg->buf);
break;
}
printf("%s\n",recvmsg->buf);
}
}
// while 退出,关闭socket
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。