当然你要先有一个云服务器,腾讯云比阿里云便宜一点,所以就用腾讯云了
问了师兄买这个98的就行,选择CentOS,不要选Ubuntu,因为
嗯,大概就是这样
云服务器当然是作为服务端
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Using:./server port\nExample:./server 5005\n\n");
return -1;
}
int listenfd;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(atoi(argv[1]));
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
{
perror("bind");
close(listenfd);
return -1;
}
if (listen(listenfd, 5) != 0)
{
perror("listen");
close(listenfd);
return -1;
}
int clientfd;
int socklen = sizeof(struct sockaddr_in);
struct sockaddr_in clientaddr;
clientfd = accept(listenfd, (struct sockaddr *)&clientaddr, (socklen_t *)&socklen);
printf("客户端%s已连接\n", inet_ntoa(clientaddr.sin_addr));
char buffer[1024];
while (true)
{
int iret;
memset(buffer, 0, sizeof(buffer));
if ((iret = recv(clientfd, buffer, sizeof(buffer), 0)) <= 0)
{
printf("iret=%d\n", iret);
break;
}
printf("receive text:%s\n", buffer);
strcpy(buffer, "ok");
if ((iret = send(clientfd, buffer, strlen(buffer), 0)) <= 0)
{
perror("send");
break;
}
printf("send text:%s\n", buffer);
}
close(listenfd);
close(clientfd);
}
在自己的Linux上写客户端代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Using:./client ip port\nExample:./client 127.0.0.1 5005\n\n");
return -1;
}
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
struct hostent *h;
if ((h = gethostbyname(argv[1])) == 0)
{
printf("gethostbyname failed.\n");
close(sockfd);
return -1;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
memcpy(&servaddr.sin_addr, h->h_addr, h->h_length);
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
{
perror("connect");
close(sockfd);
return -1;
}
char buffer[1024];
for (int i = 0; i < 3; i++)
{
int iret;
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "I am Monster number %d",i + 1);
if ((iret = send(sockfd, buffer, strlen(buffer), 0)) <= 0)
{
perror("send");
break;
}
printf("send text:%s\n", buffer);
memset(buffer, 0, sizeof(buffer));
if ((iret = recv(sockfd, buffer, sizeof(buffer), 0)) <= 0)
{
printf("iret=%d\n", iret);
break;
}
printf("receive text:%s\n", buffer);
}
close(sockfd);
}
要在防火墙这里自定义出用于通信的端口
在云服务器运行服务程序
[lighthouse@VM-4-11-centos bin]$ ./server 6666
客户端114.213.236.99已连接
receive text:I am Monster number 1
send text:ok
receive text:I am Monster number 2
send text:ok
receive text:I am Monster number 3
send text:ok
iret=0
在Linux运行客户程序
redwall@redwall-G3-3500:~/Test/socket_test/bin$ ./client 1.116.137.21 6666
send text:I am Monster number 1
receive text:ok
send text:I am Monster number 2
receive text:ok
send text:I am Monster number 3
receive text:ok
注意客户端连接的是服务端的公网IP