之前寫的那個socket太次了。只能連乙個,所以優化了一下。我管他叫superserver。
**獻上:
#include
#include
#include
#include
#include
#include
#include
intmain
(int argc,
const
char
* ar**)
int fd=
socket
(af_inet,sock_stream,0)
;if(fd<0)
struct sockaddr_in addr;
addr.sin_family=af_inet;
addr.sin_addr.s_addr=
inet_addr
(ar**[1]
);addr.sin_port=
htons
(atoi
(ar**[2]
));int bd=
bind
(fd,
(struct sockaddr*
)&addr,
sizeof
(addr));
if(bd<0)
int li=
listen
(fd,5)
;if(li<0)
struct sockaddr_in client;
socklen_t len=
sizeof
(struct sockaddr_in)
;while(1
)printf
("client's ip:%s,port:%d\n"
,inet_ntoa
(client.sin_addr)
,ntohs
(client.sin_port));
pid_t pid;
pid=
fork()
;if(pid<0)
else
if(pid>0)
else
else}}
break
;close
(sock);}
}close
(fd)
;return0;
}
來,看下效果:
棒,哈哈哈哈。
告辭,這篇博文就是這麼開心。
python3 多程序實現socket通訊
coding utf 8 import osfrom socket import from multiprocessing import process deftalk conn while 1 迴圈通訊 try from client msg conn.recv 1024 ifnot from c...
python多程序通訊
這是看書筆記 python提供了多種程序通訊的方式,比如說queue,pipe,value array等。其中queue主要用來在多個程序之間實現通訊。pipe常用來在兩個程序之間實現通訊。queue是多程序安全佇列,queue通過put和get方法來實現多程序之間的資料傳遞。put方法用於將資料插...
多程序TCP通訊
include include include include include include include include include intcreate worker int cli fd else if pid 0 int ret recv cli fd,buff,1024,0 if r...