有一段时间没有写Linux socket程序了,最近重新拾起来吧。
创建socket server/client流程
创建socket无非就是socket–bind–listen–…..这类的一些操作,下面一张图就能很好的概括这些步骤和使用的函数了。
Socket服务端程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <iostream> #include <sys/socket.h> #include <string> #include <cstring> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> using namespace std; // 服务器绑定的IP和端口 const string kServerIP = "127.0.0.1"; const int kServerPort = 7890; // 欢迎信息 const string kHelloPromopt = "Welcome to host server!"; int main() { int server_fd = 0; int client_fd = 0; char read_buf[128]; bzero(read_buf, 128); int return_result = 0; struct sockaddr_in server_addr; struct sockaddr_in client_addr; socklen_t client_addr_size = sizeof(struct sockaddr_in); bzero(&server_addr, sizeof(struct sockaddr_in)); bzero(&client_addr, sizeof(struct sockaddr_in)); server_fd = socket(AF_INET, SOCK_STREAM, 0); if(server_fd < 0) { cout << "Create socket fd error!" << endl; return -1; } // 设置服务器的sockaddr信息 server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(kServerIP.c_str()); server_addr.sin_port = htons(kServerPort); cout << "Begin to bind server..." << endl; return_result = bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in)); if(return_result < 0) { cout << "Bind server error!" << endl; return -1; } return_result = listen(server_fd, 10); if(return_result < 0) { cout << "Listen server error!" << endl; return -1; } // 等待客户端连接 cout << "Begin to accept client..." << endl; client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_size); if(client_fd < 0) { cout << "Accept client error!" << endl; return -1; } cout << "Client addr: " << inet_ntoa(client_addr.sin_addr) << "; Client Port: " << ntohs(client_addr.sin_port) << endl; // 发送欢迎信息 ssize_t send_size = send(client_fd, kHelloPromopt.c_str(), kHelloPromopt.length(), 0); if(send_size != (ssize_t)kHelloPromopt.length()) { cout << "Send was not completed!" << endl; } // 读取客户端的回复 ssize_t read_size = read(client_fd, read_buf, 128); if(read_size < 0) { cout << "read error!" << endl; cout << "Goodbye!" << endl; } cout << "Message from client: " << read_buf << endl; cout << "All done, Bye!" << endl; close(client_fd); close(server_fd); return 0; } |
服务端的程序按照上面的图写下来就好了,其中会遇到一些htons、htonl之类的函数,具体的总结解析可以看后面的整理部分解析。
Socket客户端程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <iostream> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/types.h> #include <netinet/in.h> #include <stdlib.h> #include <string> #include <cstring> using namespace std; const string kSendMessage = "Hello Server, This is a test client!"; const string kServerIP = "127.0.0.1"; const int kServerPort = 7890; const int kClientPort = 7899; int main() { int client_fd = 0; int return_result = 0; char buf[4096]; bzero(buf, 4096); struct sockaddr_in server_addr; bzero(&server_addr, sizeof(struct sockaddr_in)); server_addr.sin_addr.s_addr = inet_addr(kServerIP.c_str()); server_addr.sin_port = htons(kServerPort); server_addr.sin_family = AF_INET; cout << "Begin to create client socket..." << endl; client_fd = socket(AF_INET, SOCK_STREAM, 0); if(client_fd < 0) { cout << "Create client socket error!" << endl; return -1; } cout << "Begin to connet to server..." << endl; return_result = connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in)); if(return_result < 0) { cout << "Connect to server error!" << endl; return -1; } int read_length = read(client_fd, buf, 128); if(read_length < 0) { cout << "read from server error!" << endl; } cout << "Message from server: " << buf << endl; int send_length = send(client_fd, kSendMessage.c_str(), kSendMessage.length(), 0); if(send_length < 0) { cout << "send to server error!" << endl; } cout << "All Done!" << endl; close(client_fd); return 0; } |
这一篇先到这里了,这里用到的一些Socket辅助函数会在下一篇博文中进行讲解,大家有兴趣的可以继续关注哦。