Linux TCP/IP通信 多客户端登陆
时间:10-02
整理:3721RD
点击:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAXPKGSIZE 2048
#define MYPORT 1001
int sockfd, new_fd;
int counter = 0;
pid_t pid;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
unsigned char buf[MAXPKGSIZE];
void str_ser(int sockfd, int client_id);
int main(int argc, char **argv)
{
int ret, sin_size;
if (!bcm2835_init())
{
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf("error to create a socket\n");
exit(0);
}
bzero(&(my_addr.sin_zero), 8);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
//printf("bind, ret = %d\n", ret);
if (ret < 0)
{
printf("error to bind socket\n");
exit(0);
}
ret = listen(sockfd, MAXLOGIN);
//printf("listen, ret = %d\n", ret);
if (ret < 0)
{
printf("error to listen\n");
exit(0);
}
printf("now server is listenning on port %d\n", MYPORT);
while (1)
{
sin_size = sizeof(struct sockaddr_in);
//printf("sin_size = %d\n", sin_size);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, (socklen_t *)&sin_size);
if (new_fd < 0)
{
printf("error to accept the ..\n");
exit(0);
}
if ((pid = fork()) < 0)
{
printf("create a fork error..\n");
exit(0);
}
else if (pid == 0)
{
close(sockfd); //关闭父进程,执行子进程
printf("Client%d has connected ...\n", counter + 1);
str_ser(new_fd, counter + 1);
printf("Client%d has disconnected ...\n", counter + 1);
close(new_fd);
exit(0);
}
else //即PID为父进程
{
counter++;
printf("the %dth client connected\n", counter);
close(new_fd);
}
}
close(sockfd);
exit(0);
}
void str_ser(int sockfd, int client_id)
{
int recvlen = 0;
int fd = 0, fd1 = 0;
while (1)
{
if ((recvlen = recv(sockfd, buf, MAXPKGSIZE, 0)) == 0) //
{
return;
}
switch(buf[2])
{
case 0x00:
send(sockfd,"welcome",strlen("welcome"),0);
break;
default:
send(sockfd, "Invalid Command", strlen("Invalid Command"), 0);
break;
}
buf[recvlen] = 0;
recvlen = 0;
}
}
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAXPKGSIZE 2048
#define MYPORT 1001
int sockfd, new_fd;
int counter = 0;
pid_t pid;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
unsigned char buf[MAXPKGSIZE];
void str_ser(int sockfd, int client_id);
int main(int argc, char **argv)
{
int ret, sin_size;
if (!bcm2835_init())
{
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf("error to create a socket\n");
exit(0);
}
bzero(&(my_addr.sin_zero), 8);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
//printf("bind, ret = %d\n", ret);
if (ret < 0)
{
printf("error to bind socket\n");
exit(0);
}
ret = listen(sockfd, MAXLOGIN);
//printf("listen, ret = %d\n", ret);
if (ret < 0)
{
printf("error to listen\n");
exit(0);
}
printf("now server is listenning on port %d\n", MYPORT);
while (1)
{
sin_size = sizeof(struct sockaddr_in);
//printf("sin_size = %d\n", sin_size);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, (socklen_t *)&sin_size);
if (new_fd < 0)
{
printf("error to accept the ..\n");
exit(0);
}
if ((pid = fork()) < 0)
{
printf("create a fork error..\n");
exit(0);
}
else if (pid == 0)
{
close(sockfd); //关闭父进程,执行子进程
printf("Client%d has connected ...\n", counter + 1);
str_ser(new_fd, counter + 1);
printf("Client%d has disconnected ...\n", counter + 1);
close(new_fd);
exit(0);
}
else //即PID为父进程
{
counter++;
printf("the %dth client connected\n", counter);
close(new_fd);
}
}
close(sockfd);
exit(0);
}
void str_ser(int sockfd, int client_id)
{
int recvlen = 0;
int fd = 0, fd1 = 0;
while (1)
{
if ((recvlen = recv(sockfd, buf, MAXPKGSIZE, 0)) == 0) //
{
return;
}
switch(buf[2])
{
case 0x00:
send(sockfd,"welcome",strlen("welcome"),0);
break;
default:
send(sockfd, "Invalid Command", strlen("Invalid Command"), 0);
break;
}
buf[recvlen] = 0;
recvlen = 0;
}
}