473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

udp connection problem

24 New Member
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 4961
horace1
1,510 Recognized Expert Top Contributor
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 New Member
i tried without bind() also. it does not work..
Feb 27 '09 #3
horace1
1,510 Recognized Expert Top Contributor
could you post your code?
Feb 27 '09 #4
saee
24 New Member
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 Recognized Expert Top Contributor
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 New Member
Thank you. It works fine.
Feb 28 '09 #7
saee
24 New Member
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

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

Similar topics

3
by: Harry | last post by:
Using Oracle 8i enterprise on win 2000 (sp3) Installed the standard configuration & whenever I make a connection it takes about 10 secs. It's running on a P1900 with 1gb Ram so no reason there...
4
by: James | last post by:
We've had a recurring problem where all of a sudden we get a DBMSSOCN General Network Error on any page that connects to SQL Server. Then we have to reboot the server and everything works fine...
1
by: Mark | last post by:
I know that DB2 LUW version 8 has "connection pooling" that provides a connection concentrator (limits the number of simultaneous connections that can occur). But does it really provide connection...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
4
by: Rahul Anand | last post by:
Getting SQL Exception when trying to implement Connection based Trasaction using SQL Helper class. I am using the follwing function to execute my stored procs: -=-=-=- ExecuteScalar(ByVal...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.