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.
7 4961 horace1 1,510
Recognized Expert Top Contributor
not sure why you have bind() in the client -
s = socket(AF_INET, SOCK_DGRAM, 0);
-
bind() //client socket
-
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
i tried without bind() also. it does not work..
horace1 1,510
Recognized Expert Top Contributor
could you post your code?
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;
}
horace1 1,510
Recognized Expert Top Contributor
your server attempted to bind after receiving a message so I modified the server slightly -
#include <iostream>
-
#include <winsock2.h>
-
-
-
using namespace std;
-
//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();
-
getchar();
-
return false;
-
}
-
-
//----------------------------------------------------------
-
while(1)
-
{
-
printf("waiting for data\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;
-
}
-
the client I made a few modifications so it would compile on my machine -
#include <iostream>
-
#include <winsock2.h>
-
-
-
using namespace std;
-
//cleint.cpp
-
#define TAR_IPAddress "192.128.1.85"
-
-
#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()
-
{
-
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() );
-
getchar();
-
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;
-
}
-
it then ran OK - the client output was -
trans buf:('q' to quit)
-
hello
-
bytes sent : 10
-
trans buf:('q' to quit)
-
this is message 2
-
bytes sent : 10
-
trans buf:('q' to quit)
-
message3
-
bytes sent : 10
-
trans buf:('q' to quit)
-
goodbye
-
bytes sent : 10
-
trans buf:('q' to quit)
-
q
-
and the server -
waiting for data
-
connection accepted
-
received from: 127.0.0.1 1200
-
Bytes Recv: hello
-
waiting for data
-
connection accepted
-
received from: 127.0.0.1 1202
-
Bytes Recv: this is meÿÿ)NÃwBNÃwPÍ@
-
waiting for data
-
connection accepted
-
received from: 127.0.0.1 1204
-
Bytes Recv: message3
-
waiting for data
-
connection accepted
-
received from: 127.0.0.1 1206
-
Bytes Recv: goodbye
-
waiting for data
-
clearly still a problem with buffer lengths
this only works with the client using localhost 127.0.0.1 not sure why
Thank you. It works fine.
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().
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |