Make your own free website on Tripod.com
« June 2012 »
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
You are not logged in. Log in
Entries by Topic
All topics  «
A good linux source code bank
Tuesday, 25 October 2005
UDP server and client
server

#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 100

int main(int argc, char *argv[]) {

int sd, rc, n, cliLen;
struct sockaddr_in cliAddr, servAddr;
static char msg[MAX_MSG] = "tesint";

sd=socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}

/* bind local server port */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(LOCAL_SERVER_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0) {
printf("%s: cannot bind port number %d \n", argv[0], LOCAL_SERVER_PORT);
exit(1);
}

printf("%s: waiting for data on port UDP %u\n",
argv[0],LOCAL_SERVER_PORT);

while(1) {
memset(msg,0x0,MAX_MSG);
cliLen = sizeof(cliAddr);
n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
n = sendto(sd, msg, strlen(msg)+1, 0, (struct sockaddr *) &cliAddr, cliLen);
printf("sent to %d bytes\n", n);
if(n<0) {
printf("%s: cannot receive or send data \n",argv[0]);
continue;
}
printf("%s: from %s:UDP%u : %s \n", argv[0],inet_ntoa(cliAddr.sin_addr),
ntohs(cliAddr.sin_port),msg);
}
return 0;
}


client
#include
#include
#include
#include
#include
#include
#include
#include /* memset() */
#include /* select() */

#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 100


int main(int argc, char *argv[]) {

int sd, rc, i, n;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;

char msg[256] = {'\0'};

/* check command line args */
if(argc<3) {
printf("usage : %s ... \n", argv[0]);
exit(1);
}

/* get server IP address (no check if input is IP address or DNS name */
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
}

printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));

remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}

/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);

rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0) {
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}


// send data
for(i=2;i rc = sendto(sd, argv[i], strlen(argv[i])+1, 0,
(struct sockaddr *) &remoteServAddr,
sizeof(remoteServAddr));

if(rc<0) {
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);

exit(1);
}

}

while (1) {
memset(msg, '\0', sizeof(msg));

n = recvfrom(sd, msg, 256, 0, (struct sockaddr *) &remoteServAddr, (socklen_t * ) sizeof(remoteServAddr) );
printf("\nrecvfrom '%s'", msg);
//sleep (1);
}
sleep(10);

return 1;

}


Posted by anandss2004 at 2:07 AM KDT
Wednesday, 21 September 2005
Japanese Learning
Mood:  blue
some thing - nani ka
some body - dare ka

no thing - nani mo
no body - dare mo

any thing - non de mo
any body - dare de mo



Posted by anandss2004 at 2:05 PM KDT
One site
Mood:  blue
I find a good source code at the location, http://lxr.linux.no/.

It seems that most of the general purpose source codes are available here.

Well, I am just trying to collect the basic code and scripts available on net here for reference.

As we know that regular development need common source code many times, you can not expect to develop the same code time and again.


Posted by anandss2004 at 12:13 PM KDT
Updated: Wednesday, 21 September 2005 12:35 PM KDT

Newer | Latest | Older