服务器主要就是能够打开命令行提供的网络端口,然后一有客户端连接上,就会向客户端发送Welcome to Our Server!
这段话。
服务器代码serverSayWelcome.c
的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
// 输出错误并且向操作系统返回1,退出程序
void errorPrint(char *errorMessage);int main(int argc,char *argv[]){// 服务器的套接字int serverSocket;// 客户端的套接字int clientSocket;// 服务器的地址结构struct sockaddr_in serverAddress;// 客户端的地址结构struct sockaddr_in clientAddress;// 客户端地址大小socklen_t clientAddress_size;// 输出到客户端的字符串char stringToClient[] = "Welcome to Our Server!";// 命令行需要输入两个参数,第二个参数需要输入端口号if(argc!=2){printf("%s needs a port!\n",argv[0]);exit(1);}// 获取服务器端的套接字serverSocket = socket(PF_INET,SOCK_STREAM,0);// 若是serverSocket等于-1,就说明出错了,需要输出"socket() wrong!"退出程序if(serverSocket==-1){errorPrint("socket() wrong!");}// 把serverAddress所有字节都填充为0memset(&serverAddress,0,sizeof(serverAddress));// 把sockaddr_in的值初始化serverAddress.sin_family = AF_INET;serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);serverAddress.sin_port = htons(atoi(argv[1]));// 把地址和端口都绑定在操作系统上边,要是失败的话,会输出错误信息,退出程序if(bind(serverSocket,(struct sockaddr*)&serverAddress,sizeof(serverAddress))==-1){errorPrint("bind() wrong!");}// 把主动套接字变成被动套接字,能够监听其他主机发起连接,失败的话,会输出错误信息,退出程序if(listen(serverSocket,5)==-1){errorPrint("listen() wrong!");}// 把客户端地址大小clientAddress_size初识化clientAddress_size = sizeof(clientAddress);// 接受到其他用户的连接,clientSocket = accept(serverSocket,(struct sockaddr*) &clientAddress,&clientAddress_size);// 出错之后,会输出报错信息,退出程序if(clientSocket==-1){errorPrint("accept() wrong!");}// 向客户端输出“Welcome to Our Server!”write(clientSocket,stringToClient,sizeof(stringToClient));// 关闭客户端套接字close(clientSocket);// 关闭服务器套接字close(serverSocket);return 0;
}void errorPrint(char *errorMessage){fputs(errorMessage,stderr);fputc('\n',stderr);exit(1);
}
gcc serverSayWelcome.c -o serverSayWelcome
进行编译,./serverSayWelcome 9190
打开9190
端口等待客户端连接。
客户端就会连接服务器,然后把string from server:
连上服务器发送过来的Welcome to Our Server!
字符串输出。
客户端代码clientGetWelcome.c
的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
void errorPrint(char *errorMessage);int main(int argc,char *argv[]){int socketNumber;struct sockaddr_in serverAddress;char getString[100];int stringLength;if(argc!=3){printf("server IP or port lacks!\n",argv[0]);exit(1);}socketNumber = socket(PF_INET,SOCK_STREAM,0);if(socketNumber==-1){errorPrint("socket() is wrong!");exit(1);}memset(&serverAddress,0,sizeof(serverAddress));serverAddress.sin_family=AF_INET;serverAddress.sin_addr.s_addr=inet_addr(argv[1]);serverAddress.sin_port=htons(atoi(argv[2]));if(connect(socketNumber,(struct sockaddr*)&serverAddress,sizeof(serverAddress))==-1){errorPrint("connect() wrong!");}stringLength = read(socketNumber,getString,sizeof(getString)-1);if(stringLength==-1){errorPrint("read() is wrong!");}printf("string from server:%s\n",getString);close(socketNumber);return 0;
}void errorPrint(char *errorMessage){fputs(errorMessage,stderr);fputc('\n',stderr);exit(1);
}
gcc clientGetWelcome.c -o clientGetWelcome
进行编译,./clientGetWelcome 127.0.0.1 9190
连接到本机(127.0.0.1
)的9190
端口,然后就看到了string from server:Welcome to Our Server!
这句话。
而服务端也退出了程序。
此文章为11月Day 9学习笔记,内容来源于极客时间《网络编程实战》。