首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于Socket编程的一些基本有关问题

2012-03-02 
关于Socket编程的一些基本问题小弟刚学Socket编程.现在只想模拟以下Socket中UDP的传输.下面有两个简单的程

关于Socket编程的一些基本问题
小弟刚学Socket编程.现在只想模拟以下Socket中UDP的传输.下面有两个简单的程序但是不知道怎么数据不能接收.

发端:
#include   <Winsock2.h>
#pragma       comment(lib,       "Ws2_32.lib ")


void   main()
{
SOCKET   sock   =   socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN   addr;
memset((char*)&addr,0,sizeof(addr));
addr.sin_family   =   AF_INET;
addr.sin_addr.s_addr   =   htonl(inet_addr( "127.0.0.1 "));
addr.sin_port   =   htons(162);
char   buff[]   =   "jsafjasf ";

while   (1)
{
int   val   =   sendto(sock,buff,strlen(buff),0,(sockaddr*)&addr,sizeof(addr));
}


closesocket(sock);



}
接端:
#include   <Winsock2.h>
#pragma       comment(lib,       "Ws2_32.lib ")
#include   <iostream.h>
#include   <assert.h>

void   main()
{
char   buff[20];
buff[0]   =   'a ';
buff[1]   =   '\0 ';
SOCKET   sock   =   socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN   addr;
memset((char*)&addr,0,sizeof(addr));
addr.sin_family   =   AF_INET;
addr.sin_addr.s_addr   =   htonl(inet_addr( "127.0.0.1 "));
addr.sin_port   =   htons(162);
int   ret   =   bind(sock,(struct   sockaddr*)&addr,sizeof(addr));
long   bytes   =   0;
struct   sockaddr_in   addr1;
int   len   =   sizeof(addr1);

while(1)
{
bytes   =   recvfrom(sock,(char*)buff,20,0,(LPSOCKADDR)   &addr1,(int*)&len);
if(bytes   ==   SOCKET_ERROR   ||   bytes   ==   0)
{
assert(bytes   ==   SOCKET_ERROR);
cout < < "error " < <endl;
}
cout < <buff < <endl;
}




}

希望各位大大指点   指点

[解决办法]
你是MFC项目还是?有个初始化的过程
WORD wVersionRequested;
WSADATA wsaData;
int iErr;
wVersionRequested = MAKEWORD( 2, 2 );
iErr = WSAStartup( wVersionRequested, &wsaData );
if ( iErr != 0 )
return;
if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return;
}
[解决办法]
流程都有问题
怎么能对

/***************SocketServer*******************/

/* Add "-lwsock32 " (without the quotes) to the linker box under Project-> Project Options-> linker options.
If you have not set up a project, you will have to do that first. Go to File-> New-> Project-> Empty Project. Then add your files by using Project-> Add to Project.
If you want to link with other windows libraries you can use the same syntax. For example, "-lgdi32 -ladvapi32 " will link to gdi32.lib and advapi32.lib.
*/
#include
#include
#pragma comment (lib, "WS2_32.lib ")
//#include

main(int argc,char *argv[])
{
int USERPORT=8000 ;
char * HOST_IP_ADDR;
if (argc <2)


{
printf( "Please Input the ipaddress of the server(127.0.0.1)Default:202.127.205.185\n ");
scanf( "%s ",HOST_IP_ADDR);
if (sizeof(HOST_IP_ADDR) <7) HOST_IP_ADDR= "202.127.205.185 ";
}else
{HOST_IP_ADDR=argv[1];
}
char buf[128];
SOCKET s,ns;
struct sockaddr_in client;
struct sockaddr_in server;
int namelen,pklen;
int status;
WSADATA wsd;
printf( "The SocketServer is running now--%s\n ",HOST_IP_ADDR);
printf( "Waiting for inputs from Client\n ");
if((status=WSAStartup(MAKEWORD(2,2),&wsd))!=0)
{
perror( "wsastartup() failed: ");
exit(1);
}
if((s=socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror( "socket failed : ");
exit(1);
}
ZeroMemory(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(USERPORT);
server.sin_addr.s_addr=htons(INADDR_ANY);
if(bind(s,(struct sockaddr *)&server,sizeof(server)) <0)
{
perror( "bind() failed: ");
exit(2);
}
if(listen(s,4)!=0)
{
perror( "listen()failed : ");
exit(3);
}
while(1)
{
namelen=sizeof(client);
if((ns=accept(s,(struct
sockaddr*)&client,&namelen))==-1)
{
perror( "accept()failed: ");
exit(4);
}
printf( "accept() successful!\n ");
for(;;)
{
if((pklen=recv(ns,buf,128,0)) <0)
{
perror( "recv() failed: ");
exit(5);
}
else
if(pklen==0)
{
printf( "recv():return FAILED,connected is shut
down ");
break;
}
else
printf( "recv():return success,packet length=%d
context is %s\n ",pklen,buf);

Sleep(1);

}
}
closesocket(ns);
closesocket(s);
printf( "server ended successfully\n ");
}
------解决方案--------------------


#include
//#include //this can be replaced by WS2_32.lib
#pragma comment (lib, "WS2_32.lib ")
#define USERPORT 8000
main(int argc,char *argv[])
{
char buf[128];
SOCKET sclient;
struct sockaddr_in server;
int namelen,pklen;
int status,ret;
char szmessage[128];
WSADATA wsd;
char ch;
char * ipaddr;
printf( "Please ensure that the SocketServer.exe is running in the server! \n ");
if (argc <2){
printf( "Please input the ip address(127.0.0.1).Default:202.127.205.185\n ");
scanf( "%s ",ipaddr);
if (sizeof(ipaddr) <7) ipaddr= "202.127.205.185 " ; //this may be replaced by argv[1]
printf( "\n ");
}
else if (argc=2)
{ ipaddr= argv[1];
}
else
{ perror( "params too much! ");
exit(1);}
if((status=WSAStartup(MAKEWORD(2,2),&wsd))!=0)
{
perror( "wsastartup() failed: ");
exit(1);
}
if((sclient=socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror( "socket failed : ");
exit(1);
}
ZeroMemory(&server,sizeof(server));
ZeroMemory(szmessage,sizeof(szmessage));
server.sin_family=AF_INET;
server.sin_port=htons(USERPORT);

server.sin_addr.s_addr=inet_addr(ipaddr);
if(connect(sclient,(struct sockaddr
*)&server,sizeof(server)) <0)
{ printf( "%s ",ipaddr);
perror( "connect failed : ");
printf( "%d ",WSAGetLastError());

exit(3);
}
else
{ printf( "%s%s ",ipaddr, " connect OK!\n ");
}
namelen=sizeof(sclient);

while(1)
{

ZeroMemory(szmessage,sizeof(szmessage));
printf( "Press q to exit or input string(size <128) sent to server: ",szmessage);

scanf( "%s ",szmessage);
printf( "\n ");
if (szmessage== "q ") break;
// send(sclient,szmessage,sizeof(szmessage),0);


if(send(sclient,szmessage,sizeof(szmessage),0) <0)
{
perror( "send() failed: ");
break;
}


printf( "send return success!\n context is %s \n ",szmessage);
Sleep(10);


}


closesocket(sclient);
WSACleanup();
printf( "server ended successfully!Input any key to exit\n ");
scanf( "%c ",&ch);
return 0;
}

热点排行