Mail Archives: cygwin/2005/05/08/08:26:17
--------------000501060700040709060109
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Hi,
The codes I have attached are similar to a eariler reported problem
References:
<Pine DOT SOL DOT 4 DOT 33 DOT 0112060615220 DOT 15529-100000 AT azure DOT engin DOT umich DOT edu>
However I have my program working either in Solaris or Cygwin only. If
the server is on solaris and client on cygwin and vice versa , the
packet is lost. I cant figure out what the problem could be. I have also put
memset(&su_addr, 0, sizeof(su_addr)); /* server addr info */
su_addr.sin_family = AF_INET;
su_addr.sin_port = htons(MYPORT);
su_addr.sin_addr.s_addr = inet_addr(u_addr);
This should have solved the probelem but it didnt.
Could youhelp me in this regards.
With warm regards.
Nakul
--------------000501060700040709060109
Content-Type: text/plain;
name="eclient.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="eclient.c"
/*
* UDPclient.c -- a UDP socket client
*
* Solaris Build:
* >gcc UDPclient.c -lnsl -lsocket -lresolv -o UDPclient
*
* Invocation:
* >./UDPclient
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "udp_ack.h"
int main(int argc, char *argv[])
{
int sfd,n,remaining,port,MYPORT; /* the socket for communication */
struct sockaddr_in su_addr, m_addr; /* s(erver) and m(y) addr data */
char buf[1024];
struct timeval tmo;
fd_set rset;
unsigned short plen;
char *u_addr;
switch (argc){
case 1: printf("usage: code [port] [address]\n");
printf("Taking default values port=3490 and address=127.0.0.1\n");
MYPORT=3490;
u_addr="127.0.0.1";
break;
case 2: port=atoi(argv[1]);
printf("port is %d\n",port);
if((port<3490) || (port>3499))
{
printf("Port entered is not in range(3490-3499), taking default value\n");
MYPORT=3490;
}
else MYPORT=port;
printf("Taking default address=127.0.0.1\n");
u_addr="127.0.0.1";
break;
default:port=atoi(argv[1]);
printf("successful\n");
MYPORT=port;
u_addr=argv[2];
break;
}
memset(&m_addr, 0, sizeof(m_addr)); /* my address information */
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(0); /* 0 ==> assign me a port */
m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&su_addr, 0, sizeof(su_addr)); /* server addr info */
su_addr.sin_family = AF_INET;
su_addr.sin_port = htons(MYPORT);
su_addr.sin_addr.s_addr = inet_addr(u_addr);
printf("Ready to Transmit Data to server \n");
printf("Server details:ADDRESS: %s PORT:%d\n",u_addr,MYPORT);
printf("Client details:ADDRESS: %s PORT:%d\n",inet_ntoa(m_addr.sin_addr),ntohs(m_addr.sin_port));
printf("\nType your message here\n");
/**** open the UDP socket */
if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
return(-1);
}
/**** bind to local UDP port (randomly assigned) */
if (bind(sfd, (struct sockaddr *)&m_addr, sizeof(m_addr)) < 0)
{
perror("bind");
return(-1);
}
/**** send each line from stdin as a separate message to server */
while (fgets(buf, sizeof(buf), stdin) != NULL)
{
n = strlen(buf) + 1; /* include the EOS! */
sendto(sfd, buf, n, 0, (struct sockaddr *)&su_addr, sizeof(su_addr));
for (remaining = NRETRIES; remaining > 0; remaining--)
{
tmo.tv_sec = 0;
tmo.tv_usec = MSEC_TMO * 1000;
FD_ZERO(&rset);
FD_SET(sfd, &rset);
if (select(sfd+1, &rset, NULL, NULL, &tmo) > 0)
break;
sendto(sfd, buf, n, 0, (struct sockaddr *)&su_addr, sizeof(su_addr));
}
if (remaining <= 0)
{
perror("sendto");
return(-1);
}
if (recv(sfd, buf, sizeof(buf), 0) < 0)
{
perror("recv");
return(-1);
}
printf("Received: ");
fputs(buf, stdout);
}
/**** close the socket */
close(sfd);
}
--------------000501060700040709060109
Content-Type: text/plain;
name="eserver.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="eserver.c"
/*
* UDPechoserver.c -- a UDP socket server
*
* Solaris Build:
* >gcc UDPechoserver.c -lnln -lsocket -lresolv -o UDPechoserver
*
* Invocation:
* >./UDPechoserver >out.txt&
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int sfd; /* the socket for communication */
int len, n, port,MYPORT,m=1;
struct sockaddr_in su_addr; /* my s(erver) address data */
struct sockaddr_in c_addr; /* c(lient) address data */
char buf[1024];
switch (argc){
case 1: printf("usage: code [port] \n");
printf("Taking default values port=3490\n");
MYPORT=3490;
break;
case 2: port=atoi(argv[1]);
printf("port is %d\n",port);
if((port<3490) || (port>3499))
{
printf("Port entered is not in range(3490-3499), taking default value\n");
MYPORT=3490;
}
else MYPORT=port;
break;
default:port=atoi(argv[1]);
printf("port is %d\n",port);
MYPORT=port;
m=strcmp(argv[2],"-v");
if(m==0) printf("VERBOSE MODE\n");
break;
}
memset(&su_addr, 0, sizeof(su_addr)); /* my address info */
su_addr.sin_family = AF_INET;
su_addr.sin_port = htons(MYPORT);
su_addr.sin_addr.s_addr = htonl(INADDR_ANY);
/**** open the UDP socket */
if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
return(-1);
}
/**** bind to my local port number */
if ((bind(sfd, (struct sockaddr *)&su_addr, sizeof(su_addr)) < 0))
{
perror("bind");
return(-1);
}
/**** receive each message on the socket, printing on stdout */
while (1)
{
memset(&c_addr, 0, sizeof(su_addr));
len = sizeof (c_addr);
n = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&c_addr, &len);
if (n < 0)
{
perror("recvfrom");
return(-1);
}
if(m==0)printf("%s %d %s\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port),buf);
else {
fputs(buf, stdout);
fflush(stdout);
}
(void) sendto(sfd, buf, n, 0, (struct sockaddr *)&c_addr, len);
}
}
--------------000501060700040709060109
Content-Type: text/plain; charset=us-ascii
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
--------------000501060700040709060109--
- Raw text -