برنامه ای که نوشتم برای دریافت و ارسال اطلاعات توسط LAN به صورت UDP است. قسمت ارسال اطلاعات کار می کند ولی در هنگام دریافت اطلاعات با error 10054 متوقف می شود.
با تشکر
برنامه در Visual Basic 2012 نوشته شده است و سیستم Windows7 ServisePack1 و 64bit است.
#include<stdio.h>
#include<WinSock2.h>
#include<winsock.h>
#pragma comment(lib,"ws2_32.lib")
#define SERVER "192.168.1.59"
#define BUFLEN 512
#define PORT 27015
int main(void)
{
struct sockaddr_in si_other;
int s, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN] = "" ;
WSADATA wsa;
//Initialise winsock
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
//create socket
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
//setup address structure
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(27015);
si_other.sin_addr.s_addr = inet_addr(SERVER);
//start communication
while(1)
{
printf("Enter message : ");
gets(message);
int m = sizeof(si_other);
//send the message
if (sendto(s, message, sizeof(message) , 0 , (struct sockaddr *) &si_other, m) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
//receive a data and print it
memset(buf,'\0', BUFLEN);
//try to receive some data, this is a blocking call
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &m) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d" , WSAGetLastError());
}
puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}



پاسخ با نقل قول
