关于Socket编程的一些基本有关问题
关于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)
小弟刚学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)