LINUX SOCKET TCP 通信笔记2
经过了一个多星期的学习,我在这里总结一下关于Linux,Windows的socket TCP通信的不同情形。
- server,client之间的通信
- Socket之TCP全双工Server-Client通信
- client,client之间的通信
- 一个小应用-------网络聊天室
- Linux与Windows之间的socket通信
- socket实现两台pc之间的数据传输功能,包括windows到linux,TCP/IP
一、server,client之间的通信
server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 5678
#define BUFFER_SIZE 1024
#define MAX_QUEUE 20
int main(int argc, char const *argv[])
{
//define server_fd
int server_fd = socket(AF_INET,SOCK_STREAM,0);
//define sockaddr_in
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
server_sockaddr.sin_port = htons(PORT);
//define client socket
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
//bind ip success return 0 fault return -1
if(bind(server_fd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr)) == -1)
{
perror("bind");
exit(1);
}
printf("listen port = %d\n",PORT );
// listen ,success return 0 fault return -1
if(listen(server_fd,MAX_QUEUE) == -1)
{
perror("listen");
exit(1);
}
printf("wait for connect\n");
int connect = accept(server_fd,(struct sockaddr *)&client_addr,&length);
if(connect < 0)
{
perror("connect");
exit(1);
}
printf("client connect success\n");
char server_sendbuf[BUFFER_SIZE];
while(1)
{
fgets(server_sendbuf,sizeof(server_sendbuf),stdin) != NULL;
printf("send to client: %s\n",server_sendbuf );
send(connect, server_sendbuf,strlen(server_sendbuf),0);
int len = recv(connect,buffer,sizeof(buffer),0);
if(strcmp(buffer,"exit\n") == 0 || len<= 0)
break;
printf("data of client: %s\n",buffer );
memset(buffer,0,sizeof(buffer));
memset(server_sendbuf,0,sizeof(server_sendbuf));
}
close(connect);
close(server_fd);
return 0;
}
client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 5678
#define BUFFER_SIZE 1024
char * SERVER_IP = "127.1.1.0";
int main(int argc, char const *argv[])
{
//define socket
int client_fd = socket(AF_INET, SOCK_STREAM,0);
//define sockaddr_in
struct sockaddr_in server_addr;
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
printf("connect %s:%d\n",SERVER_IP,PORT );
//connect server success return 0 fault return -1
if (connect(client_fd, (struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)
{
perror("connect");
exit(1);
}
printf("connect server successful\n");
char sendbuf[BUFFER_SIZE];
char recvbuf[BUFFER_SIZE];
while(1)
{
fgets(sendbuf,sizeof(sendbuf),stdin) != NULL;
printf("send data to server: %s\n",sendbuf);
send(client_fd,sendbuf,strlen(sendbuf),0);
if(strcmp(sendbuf,"exit\n") == 0)
break;
recv(client_fd,recvbuf,sizeof(recvbuf),0);
printf("recv data from server: %s\n",recvbuf );
memset(sendbuf,0,sizeof(sendbuf));
memset(recvbuf,0,sizeof(recvbuf));
}
close(client_fd);
return 0;
}