#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include <time.h>
#define MAXCONN 2
#define ERRORCODE -1
#define BUFFSIZE 1024
int count_connect = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct pthread_socket
{
int socket_d;
pthread_t thrd;
};
struct sockaddr_in sockaddr;
struct sockaddr_in accept_sockaddr;
socklen_t addrlen = sizeof(accept_sockaddr);
static void* thread_recv(void *arg1)
{
char buf[BUFFSIZE];
struct pthread_socket *pt = (struct pthread_socket *) arg1;
int sd = pt->socket_d;
pthread_t thrd = pt->thrd;
time_t clock;
time(&clock);
while (1)
{
memset(buf, 0, sizeof(buf));
int rv = recv(sd, buf, sizeof(buf), 0);
if (rv < 0)
{
printf("recv error:%s \n", strerror(errno));
break;
}
if (rv == 0)
{
break;
}
printf("echo server receives contents:%s from client:%s",buf,inet_ntoa(accept_sockaddr.sin_addr));
char buf1[BUFFSIZE];
sprintf(buf1,"服务器进程PID:%d id:20201307 name:lcy echo:%s\n",getpid(),buf);
send(sd, buf1, strlen(buf1), 0);
}
pthread_cancel(thrd);
pthread_mutex_lock(&mutex);
count_connect--;
pthread_mutex_unlock(&mutex);
close(sd);
return NULL;
}
static int create_listen(int port)
{
int listen_st;
int on = 1;
listen_st = socket(AF_INET, SOCK_STREAM, 0);
if (listen_st == -1)
{
printf("socket create error:%s \n", strerror(errno));
return ERRORCODE;
}
if (setsockopt(listen_st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
{
printf("setsockopt error:%s \n", strerror(errno));
return ERRORCODE;
}
sockaddr.sin_port = htons(port);
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listen_st, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) == -1)
{
printf("bind error:%s \n", strerror(errno));
return ERRORCODE;
}
if (listen(listen_st, 5) == -1)
{
printf("listen error:%s \n", strerror(errno));
return ERRORCODE;
}
return listen_st;
}
int run_server(int port)
{
int listen_st = create_listen(port);
pthread_t send_thrd, recv_thrd;
struct pthread_socket ps;
int accept_st;
memset(&accept_sockaddr, 0, addrlen);
if (listen_st == -1)
{
return ERRORCODE;
}
printf("20201307lcy server start \n");
while (1)
{
accept_st = accept(listen_st, (struct sockaddr*) &accept_sockaddr,&addrlen);
if (accept_st == -1)
{
printf("accept error:%s \n", strerror(errno));
return ERRORCODE;
}
send(*(int *) &accept_st, "20201307lcy server begins \n", strlen("20201307lcy server begins \n"), 0);
if (count_connect >= MAXCONN)
{
printf("connect have already be full! \n");
close(accept_st);
continue;
}
pthread_mutex_lock(&mutex);
count_connect++;
pthread_mutex_unlock(&mutex);
ps.socket_d = accept_st;
if (pthread_create(&recv_thrd, NULL, thread_recv, &ps) != 0)
{
printf("create thread error:%s \n", strerror(errno));
break;
}
pthread_detach(recv_thrd);
}
close(accept_st);
close(listen_st);
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage:port,example:8080 \n");
return -1;
}
int port = atoi(argv[1]);
if (port == 0)
{
printf("port error! \n");
}
else
{
run_server(port);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#define BUFFSIZE 1024
#define ERRORCODE -1
static void *thread_send(void *arg)
{
char buf[BUFFSIZE];
int sd = *(int *) arg;
while (1)
{
memset(buf, 0, sizeof(buf));
read(STDIN_FILENO, buf, sizeof(buf));
if (send(sd, buf, strlen(buf), 0) == -1)
{
printf("send error:%s \n", strerror(errno));
break;
}
}
return NULL;
}
static void *thread_recv(void *arg)
{
char buf[BUFFSIZE];
int sd = *(int *) arg;
while (1)
{
memset(buf, 0, sizeof(buf));
int rv = recv(sd, buf, sizeof(buf), 0);
if (rv <= 0)
{
if(rv == 0)
{
printf("server have already full !\n");
exit(0);
}
printf("recv error:%s \n", strerror(errno));
break;
}
printf("\nclient receives contents from server :\n%s", buf);
}
return NULL;
}
int run_client(char *ip_str, int port)
{
int client_sd;
int con_rv;
pthread_t thrd1, thrd2;
struct sockaddr_in client_sockaddr;
client_sd = socket(AF_INET, SOCK_STREAM, 0);
if (client_sd == -1)
{
printf("socket create error:%s \n", strerror(errno));
return ERRORCODE;
}
memset(&client_sockaddr, 0, sizeof(client_sockaddr));
client_sockaddr.sin_port = htons(port);
client_sockaddr.sin_family = AF_INET;
client_sockaddr.sin_addr.s_addr = inet_addr(ip_str);
con_rv = connect(client_sd, (struct sockaddr*) &client_sockaddr,
sizeof(client_sockaddr));
if (con_rv == -1)
{
printf("connect error:%s \n", strerror(errno));
return ERRORCODE;
}
if (pthread_create(&thrd1, NULL, thread_send, &client_sd) != 0)
{
printf("thread error:%s \n", strerror(errno));
return ERRORCODE;
}
if (pthread_create(&thrd2, NULL, thread_recv, &client_sd) != 0)
{
printf("thread error:%s \n", strerror(errno));
return ERRORCODE;
}
pthread_join(thrd2, NULL);
pthread_join(thrd1, NULL);
close(client_sd);
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage:ip port,example:127.0.0.1 8080 \n");
return ERRORCODE;
}
int port = atoi(argv[2]);
char *ip_str = argv[1];
run_client(ip_str,port);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术