sock.bind(('''',PORT)) sock.listen(5) #等待连接 尝试: 而True: newSocket,address = sock.accept() print" connected from ;,地址 而True: receivedData = newSocket.recv(8192) 如果没有收到数据: 休息 打印"收到:",receivedData newSocket.sendall(receivedData) newSocket.close() 打印与...断开连接,地址 终于: sock.close() -------------------------------- ------------------------------------- C echo客户端(包括省略) ------------------------------- #定义DEFAULT_PROTOCOL 0 #define PORT 8702 #define HOSTlocalhost / * #define HOST" 127.0.0.1" * / / * #define HOST" cmopc018" * / #define TYPE SOCK_STREAM int main(无效) { struct hostent * phe; struct sockaddr_in sin; int s; s = socket(PF_INET,TYPE,DEFAULT_PROTOCOL); if(s< 0) raise_report(LEVEL_FATAL," tiny_echo",不能创建套接字: %s",strerror(errno) )); / * *连接插座 * / memset( & sin,0,sizeof sin); sin.sin_family = AF_INET; sin.sin_port = PORT; if((phe = gethostbyname(HOST))) { memcpy(& sin.sin_addr,phe-> h_addr_list [0],phe-> h_length); raise_report(LEVEL_INFO," tiny_echo"," addr is%X", sin.sin_addr); } else if((sin.sin_addr.s_addr = inet_addr(HOST))== INADDR_NONE) raise_report(LEVEL_FATAL," tiny_echo"," can) '' TG et \%s \> 主机条目,主机; if(connect(s,(struct sockaddr *)& ; sin,sinof sin)< 0) raise_report(LEVEL_FATAL," tiny_echo",无法连接到 %s。%d:%s",HOST,sin.sin_port ,strerror(errno)); raise_report(LEVEL_INFO," tiny_echo"," CONNECTED"); 返回0; } ---------------------------------- -------------------------------------------- - Nick Keighley well I assumed sockets actually worked I was guessing the slightdifferences in the interfaces caused me to set the two ends upslightlydifferently. Python Echo Server------------------import socket DEFAULT_PROTOCOL = 0PORT = 8702 sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM,DEFAULT_PROTOCOL)sock.bind (('''', PORT))sock.listen (5) # wait for a connectiontry:while True:newSocket, address = sock.accept ()print "connected from", addresswhile True:receivedData = newSocket.recv (8192)if not receivedData:breakprint "received: ", receivedDatanewSocket.sendall (receivedData)newSocket.close()print "disconnected from", addressfinally:sock.close ()---------------------------------------------------------------------C echo client (includes omitted)-------------------------------#define DEFAULT_PROTOCOL 0#define PORT 8702#define HOST "localhost"/* #define HOST "127.0.0.1" *//* #define HOST "cmopc018" */#define TYPE SOCK_STREAMint main (void){struct hostent *phe;struct sockaddr_in sin;int s; s = socket (PF_INET, TYPE, DEFAULT_PROTOCOL);if (s < 0)raise_report (LEVEL_FATAL, "tiny_echo", "can''t create socket:%s", strerror (errno));/** connect to the socket*/ memset (&sin, 0, sizeof sin);sin.sin_family = AF_INET;sin.sin_port = PORT;if ((phe = gethostbyname (HOST))){memcpy (&sin.sin_addr, phe->h_addr_list[0], phe->h_length);raise_report (LEVEL_INFO, "tiny_echo", "addr is %X",sin.sin_addr);}elseif ((sin.sin_addr.s_addr = inet_addr (HOST)) == INADDR_NONE)raise_report (LEVEL_FATAL, "tiny_echo", "can''t get \"%s\"host entry", HOST); if (connect (s, (struct sockaddr *)&sin, sizeof sin) < 0)raise_report (LEVEL_FATAL, "tiny_echo", "can''t connect to%s.%d: %s", HOST, sin.sin_port, strerror (errno)); raise_report (LEVEL_INFO, "tiny_echo", "CONNECTED"); return 0;}--------------------------------------------------------------------------------Nick Keighley 这篇关于Python&lt; - &gt; C通过插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 10:48