网络编程client和server
翻译了那么多章《beej’s guide network to programming》我们有必要自己动手写一个简单的客户端和服务器端的程序!
client side:
getaddrinfo() -> socket() -> connect() -> recv()
code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define servip "192.168.56.112"
#define servport "2349"
int main(int argc, char *argv[])
{
struct addrinfo hints, *res;
int status;
int sockfd;
int connsta;
int recvsta;
char buffer[1024];
int len;
char ipstr[inet_addrstrlen];
void *addr;
if (argc != 3) {
fprintf(stderr, "usage: not server ip and port!");
return 1;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = af_unspec;
hints.ai_socktype = sock_stream;
status = getaddrinfo(argv[1], argv[2], &hints, &res);
if (status != 0) {
fprintf(stderr, "error, server ip and port!");
return 2;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == 0) {
fprintf(stderr, "create, socket fail!");
return 3;
}
connsta = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (connsta != 0) {
fprintf(stderr, "connect, remote host fail!");
return 4;
}
else {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
addr = &(ipv4->sin_addr);
inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
printf("linked: host = %s, port = %d\n", ipstr, ntohs(ipv4->sin_port));
}
recvsta = recv(sockfd, buffer, sizeof(buffer), 0);
printf("\n%s\n", buffer);
freeaddrinfo(res);
close(sockfd);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define servip "192.168.56.112"
#define servport "2349"
int main(int argc, char *argv[])
{
struct addrinfo hints, *res;
int status;
int sockfd;
int connsta;
int recvsta;
char buffer[1024];
int len;
char ipstr[inet_addrstrlen];
void *addr;
if (argc != 3) {
fprintf(stderr, "usage: not server ip and port!");
return 1;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = af_unspec;
hints.ai_socktype = sock_stream;
status = getaddrinfo(argv[1], argv[2], &hints, &res);
if (status != 0) {
fprintf(stderr, "error, server ip and port!");
return 2;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == 0) {
fprintf(stderr, "create, socket fail!");
return 3;
}
connsta = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (connsta != 0) {
fprintf(stderr, "connect, remote host fail!");
return 4;
}
else {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
addr = &(ipv4->sin_addr);
inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
printf("linked: host = %s, port = %d\n", ipstr, ntohs(ipv4->sin_port));
}
recvsta = recv(sockfd, buffer, sizeof(buffer), 0);
printf("\n%s\n", buffer);
freeaddrinfo(res);
close(sockfd);
return 0;
}
server side:
getaddrinfo() -> socket() -> bind() -> listen() -> accept() -> send()
code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#define servport "2349"
int main(int argc, char *argv[])
{
struct addrinfo hints, *res;
int status;
int sockfd;
int connfd;
struct sockaddr_in cliaddr;
int sendsta;
char *msg;
if (argc != 2) {
fprintf(stderr, "usage: not found read file");
return 1;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = af_unspec;
hints.ai_socktype = sock_stream;
hints.ai_flags = ai_passive;
status = getaddrinfo(null, servport, &hints, &res);
if (status != 0) {
fprintf(stderr, "getaddrinfo, fail!");
return 2;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
listen(sockfd, 5);
printf("======== please wait client =========\n");
while(1) { // loop forever!
char ipstr[inet_addrstrlen];
void *addr;
int len = sizeof(cliaddr);
connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &len);
/* view client ip */
struct sockaddr_in *ipv4 = (struct sockaddr_in *)&cliaddr;
addr = &(ipv4->sin_addr);
inet_ntop(af_inet, addr, ipstr, sizeof(ipstr));
printf("client: %s\n", ipstr);
/* copy data */
msg = "hello world!";
sendsta = send(connfd, msg, strlen(msg), 0);
close(connfd);
}
close(sockfd);
return 0;
}
摘自 xiaobin_hlj80的专栏
推荐阅读
-
python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
-
python网络编程之TCP通信实例和socketserver框架使用例子
-
python实现的udp协议Server和Client代码实例
-
linux网络编程用到的网络函数详解用和使用示例
-
49.Qt-网络编程之QTCPSocket和QTCPServer(实现简易网络调试助手)
-
C++网络编程 卷1 运用ACE和模式消除复杂性——笔记1
-
python网络编程之多线程同时接受和发送
-
swoole学习(二)----搭建server和client
-
网络编程client和server
-
网络编程中重要的几个数据结构和函数