472,110 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

udp connection problem

24
hello group,
i'm trying to send a socket over udp. i'm doing it on same machine with loopback address. i wrote a server code n a client code. when i run them both, there should be a connection when i send text from client to server. client window shows sent complete n server window says waiting for connection that means recvfrom() is hanged or blocked. it does not go beyond recvfrom(). i tried connect() also from client. no luck.
my pseudo code is,

//client.c
while(1){
create server socket
create client socket
s = socket(AF_INET, SOCK_DGRAM, 0);
bind() //client socket
sendto(s,buf,10,0,server,sizeof());
}


//server.c

creat server socket
s= socket()
bind(server)
while(1){
"waiting for connection"
recvfrom(s,buf,10,0,client,length);
}

please tell wer am i doing wrong.
n one more,
i have such a code for TCp also. it works fine with loopback addr. two windows on same machine talk to each other. but it does not work with network addr. like if i replace 127.0.0.1 with 192.168.1.85 it gives error.
Feb 27 '09 #1
7 4839
horace1
1,510 Expert 1GB
not sure why you have bind() in the client
Expand|Select|Wrap|Line Numbers
  1. s = socket(AF_INET, SOCK_DGRAM, 0);
  2. bind() //client socket
  3. sendto(s,buf,10,0,server,sizeof());
unless you are going to send datagrams both ways only the receiver uses bind()

for examples of TCP and UCP clients and servers see
http://www.linuxhowtos.org/C_C++/socket.htm
Feb 27 '09 #2
saee
24
i tried without bind() also. it does not work..
Feb 27 '09 #3
horace1
1,510 Expert 1GB
could you post your code?
Feb 27 '09 #4
saee
24
sure. thanks for ur reply.

//cleint.cpp
#define TAR_IPAddress "192.128.1.85"

int main()
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKADDR_IN server, client; //Socket address information
SOCKET s;
struct hostent *hp;
int err, b;
int bytesSent, bytesRecv;
char buf[100] = "hi hello", k[2], recvbuf[100] ;
char host_name[156]; //host name of this machine
int addr_len = sizeof(struct sockaddr);



//--- INITIALIZATION -----------------------------------
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 ) {
printf("WSAStartup error %ld", WSAGetLastError());
WSACleanup();
return false;
}
//---------------------------------------------------------


//---- creat a server struct.--------
server.sin_family = AF_INET; // address family Internet
server.sin_port = htons (SERVER_PORT); //Port to connect on
server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP

//--------------------------------------------------------

//---- creat a client struct---------------------------------
client.sin_family = AF_INET; // address family Internet
client.sin_port = htons (0); //Port to connect on
client.sin_addr.s_addr = inet_addr(IPAddress); //host IP
//--------------------------------------------------------


while(1) {
// ---- create SOCKET--------------------------------------
s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
if (s == INVALID_SOCKET)
{
printf("socket error %ld" , WSAGetLastError() );
_getch();
WSACleanup();
return false; //Couldn't create the socket
}
//---------------------------------------------------------

//---- SEND bytes -------------------------------------------

printf("trans buf:('q' to quit)\n");
gets(buf);
if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
break;
bytesSent = sendto (s, buf, 10, 0, (struct sockaddr*) &server, sizeof(sockaddr));
if(bytesSent < 0 ){
printf("sendto fails with : %ld\n ", WSAGetLastError());
// _getch();
WSACleanup();
}
else
printf("bytes sent : %d\n" ,bytesSent );
//-------------------------------------------------------------------------


// WSACleanup();
}
return 0;
}

//server.cpp

#define SERVER_PORT 1167
#define BUF_SIZE 4096 // block transfer size
#define QUEUE_SIZE 10
#define IPAddress "127.0.0.1" // Local to the system - Loop back address


int main()
{
int b, on = 1;
char recvbuf[10] = { }, buf[10];
SOCKET s;
struct sockaddr_in server, client; // holds IP address
WORD wVersionRequested;
WSADATA wsaData;
int err;
int bytesRecv, bytesSent;
int addr_len = sizeof(struct sockaddr);


//--- INITIALIZATION -----------------------------------
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 ) {
printf("WSAStartup error %ld", WSAGetLastError());
WSACleanup();
return false;
}
//---------------------------------------------------------

//------------ server socket ----------------------------
server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
server.sin_family = AF_INET; // address family Internet
server.sin_port = htons (SERVER_PORT); //Port to connect on
//-----------------------------------------------------------------

while (1) {

// ---- create SOCKET--------------------------------------
s = socket(AF_INET, SOCK_DGRAM, 0); //0 specifies to use appropriate protocol according to request
if (s < 0) {
printf("socket error %ld",WSAGetLastError() );
WSACleanup();
return false;
}
//--------------------------------------------------------


//---- BIND socket ----------------------------------------
b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr));
if (b == -1) {
printf("bind error\n");
printf("socket error %ld",WSAGetLastError() );
WSACleanup();
_getch();
return false;
}

//----------------------------------------------------------

printf("waiting for a connection\n");

//-----------receive bytes -------------------------------------------------------------

bytesRecv = recvfrom(s, recvbuf, 10, 0, (struct sockaddr * )&client, &addr_len );
if(bytesRecv < 0){
printf("recvfrom error : %ld", WSAGetLastError());
WSACleanup();
}

printf("connection ");
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Closed.\n");
WSACleanup();
}
else printf("accepted\n");
printf("received from: %s %d\n ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));

printf( " Bytes Recv: %s \n ", recvbuf );

//-------------------------------------------------------------


}
WSACleanup();

return 0;
}
Feb 27 '09 #5
horace1
1,510 Expert 1GB
your server attempted to bind after receiving a message so I modified the server slightly
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <winsock2.h>
  3.  
  4.  
  5. using namespace std;
  6. //server.cpp
  7.  
  8. #define SERVER_PORT 1167
  9. #define BUF_SIZE 4096 // block transfer size
  10. #define QUEUE_SIZE 10
  11. #define IPAddress "127.0.0.1" // Local to the system - Loop back address
  12.  
  13.  
  14. int main()
  15. {
  16. int b, on = 1;
  17. char recvbuf[10] = { }, buf[10];
  18. SOCKET s;
  19. struct sockaddr_in server, client; // holds IP address
  20. WORD wVersionRequested;
  21. WSADATA wsaData;
  22. int err;
  23. int bytesRecv, bytesSent;
  24. int addr_len = sizeof(struct sockaddr);
  25.  
  26.  
  27. //--- INITIALIZATION -----------------------------------
  28. wVersionRequested = MAKEWORD( 1, 1 );
  29. err = WSAStartup( wVersionRequested, &wsaData );
  30.  
  31. if ( err != 0 ) {
  32. printf("WSAStartup error %ld", WSAGetLastError());
  33. WSACleanup();
  34. return false;
  35. }
  36. //---------------------------------------------------------
  37.  
  38. //------------ server socket ----------------------------
  39. server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
  40. server.sin_family = AF_INET; // address family Internet
  41. server.sin_port = htons (SERVER_PORT); //Port to connect on
  42. //-----------------------------------------------------------------
  43.  
  44. while (1) {
  45.  
  46. // ---- create SOCKET--------------------------------------
  47. s = socket(AF_INET, SOCK_DGRAM, 0); //0 specifies to use appropriate protocol according to request
  48. if (s < 0) {
  49. printf("socket error %ld",WSAGetLastError() );
  50. WSACleanup();
  51. return false;
  52. }
  53. //--------------------------------------------------------
  54.  
  55.  
  56. //---- BIND socket ----------------------------------------
  57. b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr));
  58. if (b == -1) {
  59. printf("bind error\n");
  60. printf("socket error %ld",WSAGetLastError() );
  61. WSACleanup();
  62. getchar();
  63. return false;
  64. }
  65.  
  66. //----------------------------------------------------------
  67. while(1)
  68. {
  69. printf("waiting for data\n");
  70.  
  71. //-----------receive bytes -------------------------------------------------------------
  72.  
  73. bytesRecv = recvfrom(s, recvbuf, 10, 0, (struct sockaddr * )&client, &addr_len );
  74. if(bytesRecv < 0){
  75. printf("recvfrom error : %ld", WSAGetLastError());
  76. WSACleanup();
  77. }
  78.  
  79. printf("connection ");
  80. if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
  81. printf( "Closed.\n");
  82. WSACleanup();
  83. }
  84. else printf("accepted\n");
  85. printf("received from: %s %d\n ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
  86.  
  87. printf( " Bytes Recv: %s \n ", recvbuf );
  88. }
  89. //-------------------------------------------------------------
  90.  
  91.  
  92. }
  93. WSACleanup();
  94.  
  95. return 0;
  96. }
  97.  
the client I made a few modifications so it would compile on my machine
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <winsock2.h>
  3.  
  4.  
  5. using namespace std;
  6. //cleint.cpp
  7. #define TAR_IPAddress "192.128.1.85"
  8.  
  9. #define SERVER_PORT 1167
  10. #define BUF_SIZE 4096 // block transfer size
  11. #define QUEUE_SIZE 10
  12. #define IPAddress "127.0.0.1" // Local to the system - Loop back address
  13.  
  14.  
  15. int main()
  16. {
  17. WORD wVersionRequested;
  18. WSADATA wsaData;
  19. SOCKADDR_IN server, client; //Socket address information
  20. SOCKET s;
  21. struct hostent *hp;
  22. int err, b;
  23. int bytesSent, bytesRecv;
  24. char buf[100] = "hi hello", k[2], recvbuf[100] ;
  25. char host_name[156]; //host name of this machine
  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. //---- creat a client struct---------------------------------
  50. client.sin_family = AF_INET; // address family Internet
  51. client.sin_port = htons (0); //Port to connect on
  52. client.sin_addr.s_addr = inet_addr(IPAddress); //host IP
  53. //--------------------------------------------------------
  54.  
  55.  
  56. while(1) {
  57. // ---- create SOCKET--------------------------------------
  58. s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
  59. if (s == INVALID_SOCKET)
  60. {
  61. printf("socket error %ld" , WSAGetLastError() );
  62. getchar();
  63. WSACleanup();
  64. return false; //Couldn't create the socket
  65. }
  66. //---------------------------------------------------------
  67.  
  68. //---- SEND bytes -------------------------------------------
  69.  
  70. printf("trans buf:('q' to quit)\n");
  71. gets(buf);
  72. if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
  73. break;
  74. bytesSent = sendto (s, buf, 10, 0, (struct sockaddr*) &server, sizeof(sockaddr));
  75. if(bytesSent < 0 ){
  76. printf("sendto fails with : %ld\n ", WSAGetLastError());
  77. // _getch();
  78. WSACleanup();
  79. }
  80. else
  81. printf("bytes sent : %d\n" ,bytesSent );
  82. //-------------------------------------------------------------------------
  83.  
  84.  
  85. // WSACleanup();
  86. }
  87. return 0;
  88. }
  89.  
it then ran OK - the client output was
Expand|Select|Wrap|Line Numbers
  1. trans buf:('q' to quit)
  2. hello 
  3. bytes sent : 10
  4. trans buf:('q' to quit)
  5. this is message 2
  6. bytes sent : 10
  7. trans buf:('q' to quit)
  8. message3
  9. bytes sent : 10
  10. trans buf:('q' to quit)
  11. goodbye
  12. bytes sent : 10
  13. trans buf:('q' to quit)
  14. q
  15.  
and the server
Expand|Select|Wrap|Line Numbers
  1. waiting for data
  2. connection accepted
  3. received from: 127.0.0.1 1200
  4.   Bytes Recv: hello 
  5.  waiting for data
  6. connection accepted
  7. received from: 127.0.0.1 1202
  8.   Bytes Recv: this is me˙˙)NĂwBNĂwPÍ@ 
  9.  waiting for data
  10. connection accepted
  11. received from: 127.0.0.1 1204
  12.   Bytes Recv: message3 
  13.  waiting for data
  14. connection accepted
  15. received from: 127.0.0.1 1206
  16.   Bytes Recv: goodbye 
  17.  waiting for data
  18.  
clearly still a problem with buffer lengths
this only works with the client using localhost 127.0.0.1 not sure why
Feb 27 '09 #6
saee
24
Thank you. It works fine.
Feb 28 '09 #7
saee
24
horace1, can you please tell me why is it not working with network ip address?
Instead of loop back address i will have give another machines IP address. n it shoulld communicate. but server is not responding. it gets hung on Recvfrom().
Mar 9 '09 #8

Post your reply

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

Similar topics

1 post views Thread by Mark | last post: by
35 posts views Thread by Eric Sabine | last post: by
16 posts views Thread by crbd98 | last post: by
20 posts views Thread by fniles | last post: by
reply views Thread by leo001 | last post: by

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.