tcp套接字编程
Posted 吾乃世间奇才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tcp套接字编程相关的知识,希望对你有一定的参考价值。
操作系统:fedora/linux 34
涉及知识:
1:tcp连接过程
2:线程创建
3:文件读写
4:信号
server.c
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#define SPORT 50000
#define SIP "192.168.1.12"
#define MAX_BACKLOG 50
#define MY_MASSAGE_MAX 1024
int acc_fd;
void *read_pfun(void *arg)
{
int fd = *((int*)arg);
char message[MY_MASSAGE_MAX];
while(1)
{
memset(message, 0, sizeof(message));
if(read(fd, message, sizeof(message)) > 0)
printf("\\nYour recevie message is :%s\\n", message);
}
}
void close_fun(int signo)
{
if(SIGINT == signo)
{
close(acc_fd); //shutdown(acc_fd)
exit(0);
}
}
int main(int argc, char *argv[])
{
char send_message[MY_MASSAGE_MAX];
signal(SIGINT, close_fun);
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd == -1)
perror(argv[0]);
struct sockaddr_in server_message;
server_message.sin_family = AF_INET;
server_message.sin_port = htons(SPORT);
server_message.sin_addr.s_addr = inet_addr(SIP);
if( bind(sfd, (struct sockaddr *)&server_message, sizeof(server_message)) == -1 )
perror(argv[0]);
if((listen(sfd, MAX_BACKLOG)) == -1)
perror(argv[0]);
struct sockaddr_in client_message;
int addrlen = sizeof(client_message);
if((acc_fd = accept(sfd, (struct sockaddr*)&client_message,&addrlen)) == -1)
perror(argv[0]);
printf("the client port is %d, the client ip is %s", ntohs(client_message.sin_port), inet_ntoa(client_message.sin_addr));
pthread_t tid;
pthread_create(&tid, NULL, read_pfun, &acc_fd);
while(1)
{
memset(send_message, 0, sizeof(send_message));
printf("Enter the message to send the your friend:");
scanf("%s", send_message);
write(acc_fd, send_message,sizeof(send_message));
}
return 0;
}
client.c
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#define MY_MESSAGE_SIZE_MAX 1024
#define SPORT 50000
#define SIP "192.168.1.12"
int fd;
void *read_func(void *parg)
{
int cfd = *((int*)parg);
char recv_message[MY_MESSAGE_SIZE_MAX];
while(1)
{
memset(recv_message, 0, sizeof(recv_message));
if( read(cfd, recv_message,MY_MESSAGE_SIZE_MAX) > 0)
printf("\\nYou receive message is %s\\n", recv_message);
}
}
void close_fun(int signo)
{
if(SIGINT == signo)
{
close(fd);//shutdonw(fd)
exit(0);
}
}
int main(int argc, char * argv[])
{
signal(SIGINT, close_fun);
char send_message[MY_MESSAGE_SIZE_MAX];
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd == -1)
perror(argv[0]);
struct sockaddr_in client_message;
client_message.sin_family = AF_INET;
client_message.sin_port = htons(SPORT);
client_message.sin_addr.s_addr = inet_addr(SIP);
if( (connect(fd, (struct sockaddr *)&client_message, sizeof(client_message))) == -1)
perror(argv[0]);
pthread_t tid;
pthread_create(&tid, NULL, read_func, (void*)&fd);
while(1)
{
memset(send_message, 0, MY_MESSAGE_SIZE_MAX);
printf("Enter the send message:");
scanf("%s", send_message);
if(write(fd, (void*)send_message, MY_MESSAGE_SIZE_MAX) == -1)
perror(argv[0]);
}
}
编译
gcc server.c -o s -pthread
gcc client.c -o c -pthread
运行:
打开两个终端:
先 ./s 再 ./c
然后./s窗口会打印客户端的ip和端口
s窗口输入信息,c窗口能打印,c窗口同理
以上是关于tcp套接字编程的主要内容,如果未能解决你的问题,请参考以下文章
网络LinuxLinux网络编程-TCP,UDP套接字编程及代码示范