#include
#define PORT "8888"
#define IP "192.168.125.61"
int deal_cli_msg(int newfd, struct sockaddr_in cin)
{
char buf[128];
ssize_t res = 0;
while (1)
{
bzero(buf, sizeof(buf));
res = recv(newfd, buf, sizeof(buf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if (0 == res)
{
newfd = %d \n",newfd);
break;
}
printf("newfd = %d :%s\n",newfd, buf);
if (strcmp(buf, "quit") == 0)
{
break;
}
strcat(buf, "*_*");
if (send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(newfd);
return 0;
}
void handle(int sig)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, const char* argv)
{
if(signal(17, handle) == SIG_ERR)
{
ERR_MSG("signal");
return -1;
}
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
int reuse = 1
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.sin_addr = inet_addr(IP);
if(bind(sfd, (struct*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = -1;
pid_t cpid = -1;
while(1)
{
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("fork");
return -1;
}
close(newfd);
}
close(sfd);
return 0;
}
#include
#define PORT 8888
#define IP "192.168.125.61"
struct Climsg
{
int newfd;
struct sockaddr_in cin;
};
void* deal_cli_msg(void* arg)
{
int newfd = ((struct Climsg*)arg)->newfd;
struct sockaddr_in cin = ((struct Climsg*)arg)->cin;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
break;
}
else if(0 == res)
{
printf("客户端下线 newfd=%d\n",newfd);
break;
}
printf("newfd=%d : %s\n", newfd, buf);
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
break;
}
printf("send success\n");
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d\n", sfd);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速的被复用成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
int newfd = -1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
pthread_t tid;
struct Climsg info;
while(1)
{
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("newfd");
return -1;
}
printf("newfd=%d\n",newfd);
info.newfd = newfd;
info.cin = cin;
if(pthread_create(&tid, NULL, deal_cli_msg, (void*)&info) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tid);
}
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}