亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3042 | 回復(fù): 8
打印 上一主題 下一主題

看看socket編程問題出在哪里 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-04-29 08:08 |只看該作者 |倒序?yàn)g覽
小弟現(xiàn)在學(xué)習(xí)Socket編程。打算實(shí)現(xiàn)一個(gè)server.c接受客戶端的連接。從客戶端接收到的字符串打印出來。客戶端一直接收用戶的輸入,并且把字符串發(fā)送到服務(wù)器端上。
    但是服務(wù)器端運(yùn)行到打。骸癗ow The server receiver client information.”這句話后一直不能顯示客戶端發(fā)送的字符串。
    期待各位解答
   

    server.c代碼如下
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
                                 
#define PORT 5000                      // The port which is communicate with server

#define BACKLOG 10
#define LENGTH 512                     // Buffer length                                                                                 

int main ()
{   int sockfd;                        // Socket file descriptor

    int nsockfd;                       // New Socket file descriptor

    int num;
    int size;
    int sin_size;                      // to store struct size

  //  char sdbuf[LENGTH];                // Send buffer

    char recvbuf[LENGTH];
    struct sockaddr_in addr_local;     
    struct sockaddr_in addr_remote;   
    char sendstr[16]= {"123456789 abcde"};   
               
    /* Get the Socket file descriptor */  
    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )  
    {   
        printf ("ERROR: Failed to obtain Socket Despcritor.\n");
        return (0);
    }
    else
    {
        printf ("OK: Obtain Socket Despcritor sucessfully.\n");
    }
   
    /* Fill the local socket address struct */
    bzero(&addr_local,sizeof(addr_local));   
    addr_local.sin_family = AF_INET;           // Protocol Family

    addr_local.sin_port = htons(PORT);         // Port number

    addr_local.sin_addr.s_addr  = htons(INADDR_ANY);  // AutoFill local address


    if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
    {  
          printf ("ERROR: Failed to bind Port %d.\n",PORT);
        return (0);
    }
    else
    {
        printf("OK: Bind the Port %d sucessfully.\n",PORT);
    }
   
    /*  Listen remote connect/calling */
    if(listen(sockfd,BACKLOG) == -1)   
    {  
        printf ("ERROR: Failed to listen Port %d.\n", PORT);
        return (0);
    }
    else
    {
        printf ("OK: Listening the Port %d sucessfully.\n", PORT);
    }
   
    while(1)
    {  
        sin_size = sizeof(struct sockaddr_in);  
        if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)
        {  
            printf ("ERROR: Obtain new Socket Despcritor error.\n");
            continue;
        }
        else
        {
      printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr));
        }
        
                        
        printf("Now The server receiver client information.\n");
        size=recv(nsockfd,recvbuf,LENGTH,0);
       if(size<0)
     {
    printf("read socket information error from %",inet_ntoa(addr_remote.sin_addr));
        }
    else
    printf("receiver from  client informationis %s",recvbuf);            
     }   
     close(nsockfd);  
      
}




  client.c代碼如下

   
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 5000                      // The port which is communicate with server

#define LENGTH 256                     // Buffer length


int main(int argc, char *argv[])
{
    int sockfd;                        // Socket file descriptor

    int num;                           // Counter of received bytes  

    struct sockaddr_in remote_addr;    // Host address information

    char sendbuf[LENGTH];
    char server_addr[]="192.168.1.114";
    char writebuf[LENGTH];
  
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        printf("ERROR: Failed to obtain Socket Descriptor!\n");
        return (0);
    }
   
    /* Fill the socket address struct */
    remote_addr.sin_family = AF_INET;                   // Protocol Family

    remote_addr.sin_port = htons(PORT);                 // Port number

    inet_pton(AF_INET, server_addr, &remote_addr.sin_addr); // Net Address

    bzero(&(remote_addr.sin_zero), 8);                  // Flush the rest of struct


    /* Try to connect the remote */
    if (connect(sockfd, (struct sockaddr *)&remote_addr,  sizeof(struct sockaddr)) == -1)
    {
        printf ("ERROR: Failed to connect to the host!\n");
        return (0);
    }  
    else
    {
        printf ("OK: Have connected to the server\n");
    }

    while(1)
    {
         printf("Please input information to send\n");
         scanf("%s",&sendbuf);
        printf("your iunput information is %s. \n",sendbuf);
         send(sockfd,sendbuf,LENGTH,0);
    }
   
    close (sockfd);
    return (0);
}



   3Q

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-04-29 08:34 |只看該作者
自己先頂一個(gè)

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-04-29 08:57 |只看該作者
你把服務(wù)中的accept放到while的外邊看看

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-04-29 09:35 |只看該作者
是while的問題

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-04-29 10:25 |只看該作者
還沒有接收到數(shù)據(jù)呢,就又開始等待連接了

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-04-29 14:59 |只看該作者
你這個(gè)port設(shè)為5000,沒有問題嗎?
你自己gdb看過后,應(yīng)該sockaddr_in.sin_port=0吧!

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-04-29 18:45 |只看該作者
while中的
if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)
        {  
            printf ("ERROR: Obtain new Socket Despcritor error.\n");
            continue;
        }
        else
        {
      printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr));
        }
        
放到外面
你這個(gè)是單用戶的TCP連接,只要accept一次就好,如果想做并發(fā)的服務(wù)器,可以考慮使用子進(jìn)程,使用fork()完成并發(fā)操作,實(shí)現(xiàn)多哥客戶端同時(shí)訪問

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2008-05-12 09:39 |只看該作者
很強(qiáng)大

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2008-05-12 13:18 |只看該作者
其實(shí)接收端是收到了的,但是由于行緩沖的原因,你程序暫時(shí)還沒有把收到的數(shù)據(jù)打印出來。另外,你的發(fā)送端也有問題。
好好看看APUE和UNP吧。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP