environment:
* foo: 192.168.104.1, 執行server.c
* bar: 192.168.104.100, 執行client.c
foo$ vim server.c
#include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { int serverfd, clientfd; struct sockaddr_in server; struct sockaddr_in client; socklen_t len; server.sin_family = AF_INET; server.sin_port = htons(8080); server.sin_addr.s_addr = inet_addr("192.168.104.1"); serverfd = socket(AF_INET, SOCK_STREAM, 0); bind(serverfd, (struct sockaddr*)&server, sizeof(server)); listen(serverfd, 10); len = sizeof(client); clientfd = accept(serverfd, (struct sockaddr*)&client, &len); close(clientfd); close(serverfd); return 0; }
foo$ gcc -Wall -g server.c -o server
bar$ vim client.c
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { int serverfd; struct sockaddr_in server; serverfd = socket(AF_INET, SOCK_STREAM, 0); server.sin_family = AF_INET; server.sin_port = htons(8080); server.sin_addr.s_addr = inet_addr("192.168.104.1"); connect(serverfd, (struct sockaddr*)&server, sizeof(server)); close(serverfd); return 0; }
bar$ gcc -Wall -g client.c -o client
單步執行server.c, 在accept()被block住
foo$ cgdb ./server
這時還沒有送出任何的封包
單步執行client.c到connect()
bar$ cgdb ./client
bar在connect()時進行three-way handshake,與foo傳送SYN,SYN/ACK,ACK封包
bar關閉socket,送出FIN封包(若由client關閉socket,則由foo送出FIN封包)
沒有留言:
張貼留言