TCP一对一双向通信
服务器:
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int con_fd = -1;
void *nv_shen(void *arg)
{
int fd = con_fd;
struct sockaddr_in addr = *(struct sockaddr_in *)arg;
char *s_buf = malloc(100);
while(1)
{
memset(s_buf,0,100);
int n = recv(fd,s_buf,100,0);//recv和send配套阻塞
if(n == 0)
{
printf("【端口号:[%d],%s】: 已退出\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr));
break;
}
printf("【端口号:[%d],%s】:%s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr),s_buf);
}
}
void *nv_shen1(void *arg)
{
int fd = con_fd;
struct sockaddr_in addr = *(struct sockaddr_in *)arg;
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
//4.发消息
fgets(buf,100,stdin);
send(fd,buf,100,0);
}
}
int main(int argc,char *argv[])
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_STREAM,0);//UDP要用数据报套接字
//2.绑定IP+端口
struct sockaddr_in serve_addr, cilent_addr;//
serve_addr.sin_family = AF_INET;
serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序
socklen_t serve_len = sizeof(serve_addr);
socklen_t cilent_len = sizeof(cilent_addr);
bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);
//监听
listen(sock_fd,4);
//创建线程
pthread_t tid;
pthread_t tid1;
//通信
while(1)
{
//4.等待连接
printf("等待连接...\n");
con_fd = accept(sock_fd, (struct sockaddr *)&cilent_addr, &cilent_len);//阻塞运行
//有对端地址,怎么访问到?
cilent_addr.sin_port;
printf("端口号:[%d],%s连接成功...\n", ntohs(cilent_addr.sin_port), inet_ntoa(cilent_addr.sin_addr) );
pthread_create(&tid,NULL,nv_shen, (void *)&cilent_addr);
pthread_create(&tid1,NULL,nv_shen1, (void *)&cilent_addr);
}
}
客户端:
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int sock_fd = -1;
void *nv_shen(void *arg)
{
int fd = sock_fd;
struct sockaddr_in addr = *(struct sockaddr_in *)arg;
char *s_buf = malloc(100);
while(1)
{
memset(s_buf,0,100);
int n = recv(fd,s_buf,100,0);//recv和send配套阻塞
if(n == 0)
{
printf("【服务器:[%d],%s】: 已退出\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr));
break;
}
printf("【服务器:[%d],%s】:%s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr),s_buf);
}
}
int main(int argc,char *argv[])
{
//1.创建套接字
sock_fd = socket(AF_INET,SOCK_STREAM,0);
//2.绑定
struct sockaddr_in serve_addr, cilent_addr;//
serve_addr.sin_family = AF_INET;
serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序
socklen_t serve_len = sizeof(serve_addr);
socklen_t cilent_len = sizeof(cilent_addr);
bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);
//3.发起连接请求
connect(sock_fd,(struct sockaddr *)&serve_addr,serve_len);
//创建线程
pthread_t tid;
pthread_create(&tid,NULL,nv_shen, (void *)&serve_addr);
char *r_buf = malloc(100);
while(1)
{
memset(r_buf,0,100);
//4.发消息
fgets(r_buf,100,stdin);
send(sock_fd,r_buf,100,0);
}
}