该楼层疑似违规已被系统折叠隐藏此楼查看此楼
/*made by ebhrz*/
#include
#include
#include
#include
#include
#include
#include
/*---------global define---------*/
int cfd; /* 文件描述符 */
int recbytes;
int sin_size;
char buffer[1024] = { 0 }; /* 接受缓冲区 */
struct sockaddr_in s_add, c_add; /* ip、端口 */
unsigned short portnum = 1234;//端口可自己修改
char msg[1024];
/*-----------global define end-----*/
int set_link()
{
cfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == cfd)
{
printf("socket fail ! \r\n");
return -1;
}
printf("socket ok !\r\n");
bzero(&s_add, sizeof(struct sockaddr_in));
s_add.sin_family = AF_INET;
s_add.sin_addr.s_addr = inet_addr("127.0.0.1"); /* ip转换为4字节整形,使用时自己修改 */
s_add.sin_port = htons(portnum);
if (-1 ==
connect(cfd, (struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("connect fail !\n");
return -1;
}
return 1;
}
int rec()
{
if (-1 == (recbytes = read(cfd, buffer, 1024)))
{
printf("read data fail !\n");
return -1;
}
printf("%s\n", buffer);
if(strcmp(buffer,"bye bye")==1)
exit(0);
memset(buffer,0,1024);
}
int snd()
{
scanf("%[^\n]", msg);
if (-1 == write(cfd, msg, 32))
{
printf("write fail!\n");
return -1;
}
memset(msg, 0, 1024);
getchar();//接收遗留\n
}
int main()
{
printf("Hello,welcome to client !\n");
pid_t pid;
if (set_link() != 1)
{
printf("sth wrong");
return -1;
}
pid = fork();
if (pid < 0)
perror("fork error");
else if (pid > 0)
{
while (1)
rec();
close(cfd);
}
else
{
while (1)
snd();
close(cfd);
}
return 0;
}
/*made by ebhrz*/
#include
#include
#include
#include
#include
#include
#include
/*---------global define---------*/
int sfp, nfp;// 文件描述符
int recbytes;
int sin_size;
char buffer[1024] = { 0 };//接受缓冲区
struct sockaddr_in s_add, c_add; //ip、端口
unsigned short s_portnum = 1234;
char msg[1024];
/*-----------global define end-----*/
int set_link()
{
sfp = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sfp)
{
printf("socket fail ! \n");
return -1;
}
printf("socket ok !\n");
bzero(&c_add, sizeof(struct sockaddr_in));
s_add.sin_family = AF_INET;
s_add.sin_addr.s_addr = htonl(INADDR_ANY);//ip不定
s_add.sin_port = htons(s_portnum);
if (-1 == bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("bind fail !\r\n");
return -1;
}
printf("bind ok !\r\n");
sin_size = sizeof(struct sockaddr_in);
return 1;
}
int rec()
{
if (-1 == (recbytes = read(nfp, buffer, 1024)))
{
printf("read data fail !\n");
return -1;
}
printf("%s\n", buffer);
if(strcmp(buffer,"bye bye")==1)
exit(0);
memset(buffer,0,1024);
}
int snd()
{
scanf("%[^\n]", msg);
printf("%s", msg);
if (-1 == write(nfp, msg, 32))
{
printf("write fail!\n");
return -1;
}
memset(msg, 0, 1024);
getchar();
}
int main()
{
printf("Hello,welcome to service !\n");
pid_t pid;
if (set_link() != 1)
{
printf("sth wrong");
return -1;
}
if (-1 == listen(sfp, 5))
{
printf("listen fail !\n");
return -1;
}
nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
if (-1 == nfp)
{
printf("accept fail !\n");
return -1;
}
pid = fork();
if (pid < 0)
perror("fork error");
else if (pid > 0)
{
while (1)
snd();
close(nfp);
close(sfp);
}
else
{
while (1)
rec();
close(nfp);
close(sfp);
}
return 0;
}