socket编程的有关问题,为什么运行失败呢
socket编程的问题,为什么运行失败呢?
server端:
client端:
为什么运行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;
}
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;
}