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 4839
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..
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;
}
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().
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
3 posts
views
Thread by Harry |
last post: by
|
4 posts
views
Thread by James |
last post: by
|
1 post
views
Thread by Mark |
last post: by
|
35 posts
views
Thread by Eric Sabine |
last post: by
|
4 posts
views
Thread by Rahul Anand |
last post: by
|
5 posts
views
Thread by Suresh |
last post: by
|
16 posts
views
Thread by crbd98 |
last post: by
|
20 posts
views
Thread by fniles |
last post: by
| | | | | | | | | | | |