TCP收发信息
TCP服务器
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
// 子线程负责写数据
void *write_data(void *v)
{
int client_socket = *(int*)v;
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
write(client_socket, buf, strlen(buf));
}
}
// 主线程负责读数据
void read_data(int client_socket)
{
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(client_socket, buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("%s\n", buf);
}
}
int main(int argc, char **argv)
{
int listen_socket = init();
if (-1 == listen_socket)
return;
int client_socket = myAccept(listen_socket);
pthread_t thread;
pthread_create(&thread, NULL, write_data, (void *)&client_socket);
read_data(client_socket);
close(listen_socket);
return 0;
}
TCP客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
// 子线程负责写数据
void *write_data(void *v)
{
int client_socket = *(int*)v;
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
write(client_socket, buf, strlen(buf));
}
}
// 主线程负责读数据
void read_data(int client_socket)
{
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(client_socket, buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("%s\n", buf);
}
}
int main(int argc, char **argv)
{
// 1、创建套接字
int conn_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == conn_socket)
{
perror("创建套接字失败");
return -1;
}
// 2、连接服务器
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(atoi(argv[2])); /*服务器用的端口号 */
addr.sin_addr.s_addr = inet_addr(argv[1]); /*服务器用的IP地址 */
int ret = connect(conn_socket, (const struct sockaddr *)&addr, sizeof(addr));
if (-1 == ret)
{
perror("连接服务器失败");
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, write_data, (void *)&conn_socket);
read_data(conn_socket);
close(conn_socket);
return 0;
}
TCP客户端与客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9997
int socket_fd[2];
void *handl_client(void *v)
{
long i = (long)v;
char buf[1024] = {"asdsadsadsadsa"};
while (1)
{
int ret = read(socket_fd[i], buf, sizeof(buf)-1);
if (-1 == ret)
{
perror ("read error");
break;
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
write(socket_fd[1-i], buf, ret);
}
}
// 监听套接字
int init()
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listen_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
// 3、监听套接字
ret = listen(listen_socket, 5);
if(-1 == ret)
{
perror("监听失败");
return -1;
}
return listen_socket;
}
// 通信套接字
int myAccept(int listen_socket)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
if (-1 == client_socket)
{
perror("accept 失败");
return -1;
}
printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return client_socket;
}
int main(int argc, char **argv)
{
int listen_socket = init();
if (-1 == listen_socket)
return;
long i = 0;
while (i < 2)
{
socket_fd[i] = myAccept(listen_socket);
if (-1 == socket_fd[i])
{
continue;
}
// 开辟线程为客户端服务
pthread_t thread;
pthread_create(&thread, NULL, handl_client, (void *)i);
pthread_detach(thread); // 线程分离
i++;
}
close(listen_socket);
pthread_exit(NULL);
return 0;
}
UDP服务器
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
void handl_client(char *buf)
{
int i;
for (i = 0; i < strlen(buf)-1; i++)
{
buf[i] += 'A' - 'a';
}
}
int init()
{
int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conm_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
return conm_socket;
}
int main(int argc, char **argv)
{
int conm_socket = init();
if (-1 == conm_socket)
return;
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
buf[ret] = '\0';
handl_client(buf); // 处理
sendto(conm_socket, buf, ret, 0, (struct sockaddr *)&client_addr, len);// 回
}
return 0;
}
UDP客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
// 子线程负责写数据
void *write_data(void *v)
{
int conm_socket = *(int*)v;
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
if (-1 == ret)
{
perror ("read error");
}
if (0 == ret)
{
printf ("客户端退出\n");
break;
}
buf[ret] = '\0';
printf ("%s\n", buf);
}
}
// 主线程负责读数据
void read_data(int conm_socket, char **argv)
{
// 接收方的地址
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(atoi(argv[2])); /*服务器用的端口号 */
addr.sin_addr.s_addr = inet_addr(argv[1]); /*服务器用的IP地址 */
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
sendto(conm_socket, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
}
}
int main(int argc, char **argv)
{
// 1、创建套接字
int conn_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conn_socket)
{
perror("创建套接字失败");
return -1;
}
pthread_t thread;
pthread_create(&thread, NULL, write_data, (void *)&conn_socket);
read_data(conn_socket, argv);
close(conn_socket);
return 0;
}
并发
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 9998
int conm_socket;
typedef struct _msg
{
char buf[256];
struct sockaddr_in client_addr;
}Msg;
Msg task[100]; // 任务
int front = 0;
int rear = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
pthread_cond_t cond1; // 条件变量
void handl_client(Msg *data)
{
int i;
for (i = 0; i < strlen(data->buf)-1; i++)
{
data->buf[i] += 'A' - 'a';
}
sendto(conm_socket, data->buf, strlen(data->buf), 0, (struct sockaddr *)&(data->client_addr), sizeof(data->client_addr));// 回
}
int init()
{
int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == conm_socket)
{
perror("创建套接字失败");
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(PORT); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址, 绑定本地的所有ip地址*/
int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
if(-1 == ret)
{
perror("绑定失败");
return -1;
}
return conm_socket;
}
void *consume(void *v)
{
while (1)
{
Msg data;
pthread_mutex_lock(&mutex);
while (rear == front)
{
pthread_cond_wait(&cond1, &mutex);
}
front = (front+1)%20;
data = task[front];
pthread_mutex_unlock(&mutex);
handl_client(&data);
}
}
void produce()
{
char buf[1024];
while (1)
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
buf[ret] = '\0';
printf ("[%s:%d] %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
pthread_mutex_lock(&mutex);
if ((rear+1)%20 == front)
continue;
rear = (rear+1)%20;
strcpy(task[rear].buf, buf);
task[rear].client_addr = client_addr;
pthread_cond_broadcast(&cond1); // 广播
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, char **argv)
{
long i;
pthread_t thread;
for (i = 0; i < 8; i++)
{
pthread_create(&thread, NULL, consume, (void*)(i+1));
pthread_detach(thread);
}
pthread_cond_init(&cond1, NULL);
conm_socket = init();
if (-1 == conm_socket)
return;
produce();
return 0;
}