PF_LOCAL, PF_UNIX, PF_FILE
Local to host (pipes and file-domain)
PF_INET IP protocol family
PF_AX25 Amateur Radio AX.25
PF_IPX Novell Internet Protocol
PF_APPLETALK Appletalk DDP
PF_NETROM Amateur radio NetROM
PF_BRIDGE Multiprotocol bridge
PF_ATMPVC ATM PVCs
PF_X25 Reserved for X.25 project
PF_INET6 IP version 6
PF_ROSE Amateur Radio X.25 PLP
PF_DECnet Reserved for DECnet project
PF_NETBEUI Reserved for 802.2LLC project
PF_SECURITY Security callback pseudo AF
PF_KEY PF_KEY key management API
PF_NETLINK, PF_ROUTE
routing API
PF_PACKET Packet family
PF_ASH Ash
PF_ECONET Acorn Econet
PF_ATMSVC ATM SVCs
PF_SNA Linux SNA Project
PF_IRDA IRDA sockets
PF_PPPOX PPPoX sockets
PF_WANPIPE Wanpipe API sockets
PF_BLUETOOTH Bluetooth sockets
对于一个不再需要的套接字,可以使用close函数关闭它。 Note that if there were any calls to fork(), each process must close the sockets it knew about (the kernel keeps track of how many processes have a descriptor open), and two processes should not use the same socket at once.
/* Server code in C */#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>intmain(void){structsockaddr_instSockAddr;intSocketFD=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);if(-1==SocketFD){perror("can not create socket");exit(EXIT_FAILURE);}memset(&stSockAddr,0,sizeof(structsockaddr_in));stSockAddr.sin_family=AF_INET;stSockAddr.sin_port=htons(1100);stSockAddr.sin_addr.s_addr=INADDR_ANY;if(-1==bind(SocketFD,(conststructsockaddr*)&stSockAddr,sizeof(structsockaddr_in))){perror("error bind failed");close(SocketFD);exit(EXIT_FAILURE);}if(-1==listen(SocketFD,10)){perror("error listen failed");close(SocketFD);exit(EXIT_FAILURE);}for(;;){intConnectFD=accept(SocketFD,NULL,NULL);if(0>ConnectFD){perror("error accept failed");close(SocketFD);exit(EXIT_FAILURE);}/* perform read write operations ... */shutdown(ConnectFD,SHUT_RDWR);close(ConnectFD);}close(SocketFD);return0;}
Python实现:
fromsocketimport*fromtimeimportctimeHOST=''PORT=1100BUFSIZ=1024ADDR=(HOST,PORT)sock=socket(AF_INET,SOCK_STREAM)sock.bind(ADDR)sock.listen(5)whileTrue:print('waiting for connection')tcpClientSock,addr=sock.accept()print('connect from ',addr)whileTrue:try:data=tcpClientSock.recv(BUFSIZ)except:print(e)tcpClientSock.close()breakifnotdata:breaks='Hi,you send me :[%s] %s'%(ctime(),data.decode('utf8'))tcpClientSock.send(s.encode('utf8'))print([ctime()],':',data.decode('utf8'))tcpClientSock.close()sock.close()
/* Client code in C */#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>intmain(void){structsockaddr_instSockAddr;intRes;intSocketFD=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);if(-1==SocketFD){perror("cannot create socket");exit(EXIT_FAILURE);}memset(&stSockAddr,0,sizeof(structsockaddr_in));stSockAddr.sin_family=AF_INET;stSockAddr.sin_port=htons(1100);Res=inet_pton(AF_INET,"192.168.1.3",&stSockAddr.sin_addr);if(0>Res){perror("error: first parameter is not a valid address family");close(SocketFD);exit(EXIT_FAILURE);}elseif(0==Res){perror("char string (second parameter does not contain valid ipaddress");close(SocketFD);exit(EXIT_FAILURE);}if(-1==connect(SocketFD,(conststructsockaddr*)&stSockAddr,sizeof(structsockaddr_in))){perror("connect failed");close(SocketFD);exit(EXIT_FAILURE);}/* perform read write operations ... */shutdown(SocketFD,SHUT_RDWR);close(SocketFD);return0;}
#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<sys/socket.h>#include<sys/types.h>#include<netinet/in.h>#include<unistd.h> /* for close() for socket */intmain(intargc,char*argv[]){intsock;structsockaddr_insa;intbytes_sent,buffer_length;charbuffer[200];buffer_length=snprintf(buffer,sizeof(buffer),"Hello World!");sock=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);if(-1==sock)/* if socket failed to initialize, exit */{printf("Error Creating Socket");exit(EXIT_FAILURE);}memset(&sa,0,sizeof(sa));sa.sin_family=AF_INET;sa.sin_addr.s_addr=htonl(0x7F000001);sa.sin_port=htons(7654);bytes_sent=sendto(sock,buffer,buffer_length,0,(structsockaddr*)&sa,sizeof(structsockaddr_in));if(bytes_sent<0)printf("Error sending packet: %s\n",strerror(errno));close(sock);/* close the socket */return0;}