如何將套接字例程設置爲「發送」(第一個)或(切換)「接收」,如果數據是「發送「來自另一臺計算機(第一個)?如何設置套接字來執行「發送/接收」或「接收/發送」
感謝
通用代碼:
-(void) TcpClient{
char buffer[128];
struct sockaddr_in sin;
struct hostent *host;
int s;
host = gethostbyname("10.0.0.3");
memcpy(&(sin.sin_addr), host->h_addr,host->h_length);
sin.sin_family = host->h_addrtype;
sin.sin_port = htons(4000);
s = socket(AF_INET, SOCK_STREAM, 0);
connect(s, (struct sockaddr*)&sin, sizeof(sin));
while(1){//this is the Client sequence:
send(s, buffer, strlen(buffer), 0);//but what if the Server sends first ?? Client needs to receive here first
recv(s, buffer, sizeof(buffer), 0);
}
close(s);
}
是否需要設置解鎖? – jdl 2012-04-28 00:23:10
不,'select()'適用於阻塞和非阻塞套接字。 – 2012-04-28 01:30:13