12 Replies - 1077 Views - Last Post: 04 November 2013 - 03:17 PM Rate Topic: -----

#1 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

recvfrom doesn't work.

Posted 03 November 2013 - 07:36 AM

I've got client server applicattion based on udp protocol. recvfrom in server doesn't work. It is run on microsoft visual studio 2010. Why recvfrom doesn't work?

server code:


#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>

#define BUFLEN 512  
#define PORT 8888   

int _tmain(int argc, _TCHAR* argv[])
{
    SOCKET s;
    struct sockaddr_in server, si_other;
    int slen , recv_len;
    char buf[BUFLEN];
    WSADATA wsa;
    slen = sizeof(si_other) ;
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    printf("Initialised.\n");
    if((s = socket(PF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
    printf("Socket created.\n");
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( PORT );
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    puts("Bind done");
    printf("Waiting for data...");
    int bufsize;
	if ((bufsize=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) < 0)
    {
        printf("data can't be received");
	}
	printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
    printf("Data: %s\n" , buf);
        if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) < 0)
        {
            printf("sendto() failed with error code : %d" , WSAGetLastError());
            exit(EXIT_FAILURE);
        }
    printf("Datan sent");
 
    closesocket(s);
    WSACleanup();
     
    return 0;
}


client code:

// client_udp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<winsock2.h>
#include<stdlib.h>
const int CLIENT_PORT=25001;
int _tmain(int argc, _TCHAR* argv[])
{
   struct sockaddr_in serverAddr; 
   struct sockaddr_in clientAddr; 
   int clientSock; 
   char buf[100]; 
   int serverPort;
   int len=sizeof(struct sockaddr);
   WSADATA wsaData;
   WSAStartup(0x0202,&wsaData); 
   memset((char*)&clientAddr,0,sizeof(clientAddr));
   clientAddr.sin_family=AF_INET; 
   clientAddr.sin_addr.s_addr=INADDR_ANY;
   clientAddr.sin_port=htons((u_short)CLIENT_PORT); 
   serverAddr.sin_family=AF_INET;
   if(argc!=3)
   {
	   printf("zła liczba argumentów - powinno byc 2 adres IP i numer portu serwera");
	   return 0;   
   }
   printf("Podaj ciąg znakow ktory chcesz przeslac\n");
   scanf("%s", buf);
   serverAddr.sin_addr.s_addr=inet_addr((char*)argv[1]);
   serverPort=atoi((char*)argv[2]);
   serverAddr.sin_port=htons((u_short)serverPort);
   clientSock=socket(PF_INET,SOCK_DGRAM,0);
   if (clientSock<0){
     fprintf(stderr,"socket creating failed\n");
     return 0;
   }
   printf("socket created\n");
   if (bind(clientSock,(struct sockaddr *) &clientAddr,len)<0)
   {
     fprintf(stderr,"bind failed\n");
     exit(1);
   }
   printf("bind OK\n");
  
   
     if(sendto(clientSock,buf,strlen(buf),0,(struct sockaddr *)&serverAddr,len)<0)
	 {
		 printf("Blad sendto");
		 return 0;
	 }
     printf("data sent");
	 while(1)
	 {
		 if(recvfrom(clientSock,buf,sizeof(buf),0,(struct sockaddr *)&serverAddr,&len)>0)
	     break;
	 }
   printf("Odebrane dane %s", buf);
   closesocket(clientSock);
   WSACleanup();
   return 1;
}



This post has been edited by rozkurwiator: 03 November 2013 - 07:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: recvfrom doesn't work.

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 08:03 AM

The first question I usually ask for people running on Windows is: "Are you sure your firewall(s) have the appropriate ports open?"
Was This Post Helpful? 0
  • +
  • -

#3 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 09:13 AM

How to check it? OS is windows 7

This post has been edited by rozkurwiator: 03 November 2013 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 09:31 AM

I thing it is ok. netstat shows the port is open.
Was This Post Helpful? 0
  • +
  • -

#5 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: recvfrom doesn't work.

Posted 03 November 2013 - 10:43 AM

View Postrozkurwiator, on 03 November 2013 - 09:31 AM, said:

I thing it is ok. netstat shows the port is open.

http://www.canyouseeme.org/
Was This Post Helpful? 0
  • +
  • -

#6 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 10:49 AM

It doesn't see the port. How to fix this? In addiction i supposed to tell you I'm in local network area in university and IP adress is different from that is shown in ipconfig. I tried to set firewall properly but I coludn't.
Was This Post Helpful? 0
  • +
  • -

#7 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 03:24 PM

Help me pleeeasee :P
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 03:52 PM

Are you running both the server and the client within the same subnet? The test for canyouseeme.org is for accessing your server from outside of your network. If you are just going to be running from within your LAN, or even easier, within the same subnet in that LAN, then things get much easier.
Was This Post Helpful? 0
  • +
  • -

#9 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 05:01 PM

Could you tell the same easier :) I don't understand network exactly:D I am just a programmer ;]
Was This Post Helpful? 0
  • +
  • -

#10 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 05:38 PM

So the code is ok?
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 05:56 PM

If you don't know networking, why are you writing networking code?

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#12 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 03 November 2013 - 11:46 PM

I've already started to learn it :)/>/> I must know basics of networking in my uniwersity.

does recvfrom work on anybody's computer?try it please :)/>/> What should I fix or change?

This post has been edited by rozkurwiator: 03 November 2013 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#13 rozkurwiator  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 03-July 12

Re: recvfrom doesn't work.

Posted 04 November 2013 - 03:17 PM

Help pleaaseee :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1