读书人

socket编程的有关问题为什么运行失败

发布时间: 2012-04-11 17:42:33 作者: rapoo

socket编程的问题,为什么运行失败呢?
server端:

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <memory>#include <arpa/inet.h>#include "error.h"int main(){    int sockfd,client_fd;/*sockfd:  monitor socket;  client_fd:data transmission socket*/    struct sockaddr_in my_addr;/*local host address  message*/    struct sockaddr_in remote_addr;/*client address message*/    int ret;    do    {        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)        {            ret=SOCKER_GREAT_ERROR;            break;        }    my_addr.sin_family=AF_INET;    my_addr.sin_port=htons(SERVERPORT);    my_addr.sin_addr.s_addr = INADDR_ANY;    memset(my_addr.sin_zero,0,sizeof(my_addr.sin_zero));        if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1)        {            ret=BIND_ERROR;            break;        }        if(listen(sockfd,BACKLOG)==-1)        {            ret=LISTEN_ERROR;            break;        }        while(1)        {            socklen_t  sin_size=sizeof(struct sockaddr);            if((client_fd=accept(sockfd,(struct sockaddr*)&remote_addr,&sin_size))==-1)            {                ret=ACCEPT_ERROR;                break;            }            printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));            if (!fork())             {                 if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1)                {                    ret=SEND_ERROR;                }                close(client_fd);                exit(0);            }        }            }while(0);    if(0!=ret)        printf("run error with %d\n",ret);    return ret;}

client端:
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <memory>#include <arpa/


为什么运行client端程序时会报内存错误呢?运行方式为 ./client <hostname>
有什么问题吗?

[解决办法]
好像没看见你初始化
[解决办法]
if((host=gethostbyname(argv[1]))=NULL)
编码归范呀,
if((host=gethostbyname(argv[1]))==NULL)


[解决办法]
Windows下需要初始化
The following code fragment demonstrates how an application that supports only version 2.2 of Windows Sockets makes a WSAStartup call:

WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}



/* The WinSock DLL is acceptable. Proceed. */


[解决办法]
发生错误,打印errno这个变量值,再对照错误码表进行定位。
[解决办法]
这个在LINUX下面不太了解情况哦!~~~不过在WINDOWS下的话需要在用之前调用WSAStartup这个接口去初始化SOCKET的。
[解决办法]
linux不用WSAStartup,看看你的connect失败的话,看是不是获取的主机地址不对呢。
serverpot常量在哪定义的。
[解决办法]

探讨
才发觉error.h文件中错误定义重复了,改了下,发觉报错其实是gethostbyname()失败,为什么会失败呢,我是在Linux环境下测试的。。

读书人网 >C++

热点推荐