-1
我收到來自客戶端的消息,將其與根據比較結果發送消息進行比較。服務器發送消息(顯然),但客戶端停留在讀取過程中。消息發送,接收方沒有收到消息。套接字編程
服務器:
等待傳入連接...按下Ctrl + C來結束服務器
從127.0.0.1連接,端口54918消息收到:C隨機ES 5 入的IF IF 1er酒店
void
result(int sockfd)
{
ssize_t n;
char buf[MAXLINE];
int temp;
time_t ticks;
int i;
again:
while ((n =read(sockfd, buf, MAXLINE)> 0))
{
buf[n] = '\0';
printf("Message Recieved:%s\n",buf);
srand (time(NULL));
temp = rand() % 15+1;
printf("Ramdom es %i\n",temp);
printf("Into IF\n");
if ((strncmp (buf,"A",1) == 0) || (strncmp (buf,"a",1) == 0))
{
snprintf(buf, sizeof(buf), "Option A chosen times %i on on %.24s\r\n", temp,ctime(&ticks));
Writen(sockfd, buf, n);
printf("1st IF\n");
}
else if ((strncmp (buf,"B",1) == 0) || (strncmp (buf,"b",1) == 0))
{
snprintf(buf, sizeof(buf), "Option B chosen times %i on on %.24s\r\n", temp,ctime(&ticks));
Writen(sockfd, buf, n);
printf("2nd IF\n");
}
else
{
printf("Incorrect Input");
Close(sockfd);
break;
}
}
if (n < 0 && errno == EINTR)
goto again;
else if (n < 0)
err_sys("read error");
printf("salio de funcion result\n");
}
int
main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
/*char message[MAXLINE];*/
char recvline[MAXLINE + 1];
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);/*----------------------------------------------------*/
servaddr.sin_port = htons(5678);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
printf("Server is running on 127.0.0.1, listening on port 5678\n");
printf("\n");
printf("Waiting for incoming connections...Press Ctrl+C to end server\n");
for (; ;)
{
len = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &len);
/*Client connects to server*/
printf("\n");
printf("Connection from %s, port %d\n",
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));
result(connfd);
Close(connfd);
}
}
客戶:
連接...本地地址:127.0.0.1:54918 開始讀...... 輸入選項A或B:B
void
get_temp(FILE *fp, int sock)
{
char sendline[MAXLINE], recvline[MAXLINE];
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Writen(sock, sendline, strlen(sendline));
if (Readline(sock, recvline, MAXLINE) == 0)
err_quit("Server Terminated Prematurely");
Fputs(recvline, stdout);
}
}
int
main(int argc, char **argv)
{
int sockfd, n;
socklen_t len;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr, cliaddr;
char scale[2];
if (argc != 3)
err_quit("usage: a.out <IPaddress>");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
printf("Connect...\n");
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");
len = sizeof(cliaddr);
Getsockname(sockfd, (SA *) &cliaddr, &len);
printf("Local Address is: %s\n",
Sock_ntop((SA *) &cliaddr, sizeof(cliaddr)));
printf("Starting read...\n");
printf("Enter option A or B: ");
get_temp(stdin, sockfd);
exit(0);
}
感謝。
'Writen'做了什麼?它的最後一個參數是什麼?如果這是要發送的字節數,那麼這就是你的問題 - 你的發送者沒有發送完整的信息,但這就是你的接收者正在等待的內容。 – 2013-03-23 23:37:33
'ssize_t' \t \t \t \t \t \t 'writen(INT FD,常量無效*的vptr,爲size_t無)' { \t \t的size_t \t nleft; \t ssize_t \t \t nwritten; \t const char \t * ptr; \t ptr = vptr; \t nleft = no; \t而(nleft> 0){ \t \t如果((nwritten =寫入(FD,PTR,nleft))<= 0){ \t \t \t如果(nwritten <0 &&錯誤號== EINTR) \t \t \t \t nwritten = 0; \t \t \t \t \t其他 \t \t \t \t回報(-1); \t \t \t \t \t} \t \t nleft - = nwritten; \t \t ptr + = nwritten; \t} \t return(no); } /*端writen */ 空隙 Writen(INT FD,void *的PTR,爲size_t爲nbytes) { \t如果(writen(FD,PTR,爲nbytes)!=爲nbytes) \t \t err_sys(「writen錯誤」); } – netfreak 2013-03-23 23:54:14
這是寫的代碼。我不知道如何將它作爲代碼在評論中。如果有人能告訴我這將是偉大的! – netfreak 2013-03-23 23:56:31