473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

socket programming

24
hello group,
i'm trying udp socket program. my code works fine with local host ip. i.e 127.0.0.1
according to my knowledge, if i change the ip address n give remote host's ip, two machines should communicate. it doesn't work. after runing server i checked port engaged with netstat. it shows perticular port in use.
please tell is there an other settings that i should look for?
thenk you.
Mar 17 '09 #1
8 1579
saee
24
i couldn't find a solution for it yet. i have searched on net also. does anybody have a idea where shall i search for the solution at least?
thnks
Mar 18 '09 #2
Andr3w
42
Please can you post your code so we can see what's the problem and help you if possible?

Thanks!
Mar 18 '09 #3
saee
24
yes sure andr3w. here is my code.

Expand|Select|Wrap|Line Numbers
  1. ///////////
  2. client.cpp
  3. ////////////////////
  4.  
  5. #include <iostream>
  6. #include <winsock2.h> 
  7.  
  8. using namespace std;
  9. #define SERVER_PORT 3000
  10. #define BUF_SIZE 4096 // block transfer size  
  11. #define QUEUE_SIZE 20
  12. #define IPAddress "127.0.0.1" // Local to the system - Loop back address
  13.  
  14. //#define IPAddress "192.168.1.11"
  15.  
  16.  
  17. int main()
  18. {
  19.     WORD            wVersionRequested;
  20.     WSADATA            wsaData;
  21.     SOCKADDR_IN        server;                 //Socket address information
  22.     SOCKET            s;
  23.     int                err;
  24.     int                bytesSent;
  25.     char            buf[100] = "hi hello";
  26.     int                addr_len = sizeof(struct sockaddr);
  27.  
  28.  
  29.  
  30. //--- INITIALIZATION -----------------------------------
  31.     wVersionRequested = MAKEWORD( 1, 1 );
  32.     err = WSAStartup( wVersionRequested, &wsaData );
  33.  
  34.     if ( err != 0 ) {
  35.         printf("WSAStartup error %ld", WSAGetLastError());
  36.         WSACleanup();
  37.         return false;
  38.     }
  39. //---------------------------------------------------------
  40.  
  41.  
  42. //---- creat a server struct.--------  
  43.     server.sin_family = AF_INET; // address family Internet
  44.     server.sin_port = htons (SERVER_PORT); //Port to connect on
  45.     server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
  46.  
  47. //--------------------------------------------------------
  48.  
  49.     while(1) {
  50. // ---- create SOCKET--------------------------------------
  51.     s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
  52.     if (s == INVALID_SOCKET)
  53.     {
  54.         printf("socket error %ld" , WSAGetLastError() );
  55.         getchar();
  56.         WSACleanup();
  57.         return false; //Couldn't create the socket
  58.     }  
  59. //---------------------------------------------------------
  60.  
  61.  
  62. //---- SEND bytes -------------------------------------------
  63.  
  64.         printf("trans buf:('q' to quit)\n");
  65.         gets_s(buf);
  66.         if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
  67.             break;
  68.         bytesSent = sendto (s, buf, QUEUE_SIZE, 0, (struct sockaddr*) &server, sizeof(sockaddr));
  69.         if(bytesSent < 0 ){
  70.             printf("sendto fails with : %ld\n ", WSAGetLastError());
  71.  
  72.             WSACleanup();
  73.         }
  74.         else 
  75.             printf("bytes sent :  %d\n" ,bytesSent );
  76. //-------------------------------------------------------------------------
  77.  
  78.     }
  79.     return 0;
  80. }

Expand|Select|Wrap|Line Numbers
  1. ////////////////////////
  2. server.cpp
  3. ////////////////////////
  4.  
  5. #include <iostream>
  6. #include <winsock2.h> 
  7.  
  8. #define SERVER_PORT 3000
  9. //#define BUF_SIZE 4096  // block transfer size  
  10. #define QUEUE_SIZE 20 
  11. #define IPAddress "127.0.0.1" // Local to the system - Loop back address
  12.  
  13. //#define IPAddress "127.0.0.85"
  14.  
  15.  
  16. int main()
  17. {
  18.     WORD        wVersionRequested;
  19.     WSADATA        wsaData;
  20.     SOCKET        s; 
  21.     struct        sockaddr_in server, client;  // holds IP address 
  22.     char        recvbuf[10] = {}; 
  23.     int            b, opt; 
  24.     int            err;
  25.     int            bytesRecv;//, bytesSent;
  26.     int            addr_len = sizeof(struct sockaddr);
  27.     char        *val = new char;
  28.  
  29.  
  30. //--- INITIALIZATION -----------------------------------
  31.     wVersionRequested = MAKEWORD( 1, 1 );
  32.     err = WSAStartup( wVersionRequested, &wsaData );
  33.  
  34.     if ( err != 0 ) {
  35.         printf("WSAStartup error %ld", WSAGetLastError());
  36.         WSACleanup();
  37.         return false;
  38.     }
  39. //---------------------------------------------------------
  40.  
  41. //------------ server socket ----------------------------
  42.     server.sin_addr.s_addr = inet_addr(IPAddress);//htonl(INADDR_ANY);
  43.     server.sin_family = AF_INET; // address family Internet
  44.     server.sin_port = htons (SERVER_PORT); //Port to connect on
  45. //-----------------------------------------------------------------
  46.  
  47.  
  48. // ---- create SOCKET--------------------------------------
  49.     s = socket(AF_INET, SOCK_DGRAM, 0);    //0 specifies to use appropriate protocol according to request
  50.     if (s < 0) {
  51.         printf("socket error %ld",WSAGetLastError() );
  52.         WSACleanup();
  53.         return false;
  54.     }
  55. //--------------------------------------------------------
  56.  
  57.  
  58. //---- BIND socket ----------------------------------------
  59.     b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr)); 
  60.     if (b == -1) {
  61.         printf("socket bind error %ld",WSAGetLastError() );
  62.         WSACleanup();
  63.         getchar();
  64.         return false;
  65.     }
  66.  
  67. //----------------------------------------------------------
  68. while (1) {
  69.         printf("\nwaiting for a connection\n");    
  70.  
  71.  
  72. //-----------receive bytes -------------------------------------------------------------
  73.  
  74.         bytesRecv  = recvfrom(s, recvbuf, QUEUE_SIZE, 0, (struct sockaddr *)&client, &addr_len );
  75.  
  76.         if(bytesRecv < 0){
  77.             printf("\nbytesRecv : %d\n", bytesRecv);
  78.             printf("\nrecvfrom error : %ld\n", WSAGetLastError());
  79.             WSACleanup();
  80.         }
  81.  
  82.         printf("connection ");
  83.         if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
  84.             printf( "Closed.\n");
  85.             WSACleanup();
  86.         }
  87.         else printf("accepted\n");
  88.         printf("received from: %s \t port : %d\n ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
  89.  
  90.         printf( " Bytes Recv: %s \n ", recvbuf );
  91.  
  92. //-----------------------------------------------------------------------------------------------
  93.  
  94.  
  95.     }
  96.  
  97.     WSACleanup();
  98.     return 0;
  99. }
see, here in this code if i replace loopback address with other machines ip address it should work. i mean that is what i cud know till now. please tell me if there are also other settings i should check like firewalls. actually i have kept firewall off.
thank you again.
Mar 19 '09 #4
saee
24
i eve tried tcp client- server. same problem! :(
works with loop back address n not with network addresses.
Mar 21 '09 #5
hi..
your LAN activated Firewall means it generate TimedOut exception
so u check firewall settings tick On recommend radio button
Mar 21 '09 #6
saee
24
hi shreeram,
i tried. no luck. :(
r u sure the code is correct?
Mar 21 '09 #7
saee
24
can anybody please answer me?
Mar 23 '09 #8
saee
24
ok. let me state my problem newly.
my client n server can send n receive data from labview's udp port. but not to each other.as, it can communicate with labview, the code is correct. but i can not send data through this c application. n i can not figure out why.
Mar 24 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.