/*
must update ur records uptodate and submit to internal examination :
internal dates :
CSE-A : 8-10-2012
CSE-B : 6-10-2012
*/
must update ur records uptodate and submit to internal examination :
internal dates :
CSE-A : 8-10-2012
CSE-B : 6-10-2012
*/
W6
/* UDP
client and server application to reverse the given input sentence */
/* client program */
# include
"unp.h"
/* UDP ECHO Client:
dg_cli function*/
void dg_cli(FILE *fp,int sockfd,const SA* pservaddr,socklen_t servlen)
{
int
n;
char sendline[MAXLINE],
recvline[MAXLINE+1];
while (
Fgets(sendline,MAXLINE,fp)!=NULL)
{
sendto(sockfd, sendline,
strlen(sendline), 0, pservaddr,servlen);
n= Recvfrom(sockfd,recvline,
MAXLINE,0,NULL,NULL);
recvline[n]=0;
Fputs(recvline, stdout);
}
}
/* UDP client main function: */
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in
servaddr;
if(argc!=2)
err_quit("usage :
udpcli <Ipaddress>");
bzero(&servaddr,
sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_port =
htons(SERV_PORT);
Inet_pton(AF_INET, argv[1],
&servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM,
0);
dg_cli(stdin, sockfd, (SA*)
&servaddr, sizeof(servaddr));
exit(0);
}
/* compiling */
$ cc udp_simple_client.c
wrapsock.c –o udpreverseclient
/* execution */
$ ./
udpreverseclient
hai
Iah
/* UDP server program */
# include
"unp.h"
/* UDP echo server
dg_echo function: */
void dg_echo(int sockfd,SA*pcliaddr,socklen_t clilen)
{
int n,i,j,swp;
socklen_t len;
char mesg[MAXLINE];
for(;;){
len=clilen;
n=Recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
printf("From Client : %s reversing ..",mesg);
for(i=0,j=n-1;i<n/2;i++,j--)
{
swp=mesg[i];
mesg[i]=mesg[j];
mesg[j]=swp;
}
Sendto(sockfd,mesg,n,0,pcliaddr,len);
}
}
/*UDP echo server main
function:*/
#include"unp.h"
int main(int argc,char **argv[])
{
int sockfd;
struct sockaddr_in
servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
Bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
dg_echo(sockfd,(SA*)&cliaddr,sizeof(cliaddr));
}
/* compiling */
$ cc udp_simple_server.c
wrapsock.c –o udpreverseserver
/* output */
$./ udpreverseserver
Rev hai
W7
/* UDP
client and server application to transfer a text file*/
/*client program */
# include
"unp.h"
/* UDP ECHO Client:
dg_cli function*/
void dg_cli(FILE *fp,int sockfd,const SA* pservaddr,socklen_t servlen)
{
int
n;
char sendline[MAXLINE],
recvline[MAXLINE+1];
while ( Fgets(sendline,MAXLINE,fp)!=NULL)
{
sendto(sockfd, sendline,
strlen(sendline), 0, pservaddr,servlen);
n= Recvfrom(sockfd,recvline,
MAXLINE,0,NULL,NULL);
recvline[n]=0;
Fputs(recvline, stdout);
}
}
/* UDP client main function: */
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in
servaddr;
if(argc!=2)
err_quit("usage :
udpcli <Ipaddress>");
bzero(&servaddr,
sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_port =
htons(SERV_PORT);
Inet_pton(AF_INET, argv[1],
&servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM,
0);
dg_cli(stdin, sockfd, (SA*) &servaddr,
sizeof(servaddr));
exit(0);
}
/* compiling */
$ cc
udp_simple_file_client.c wrapsock.c –o udpfileclient
/* execution */
$ ./
udpfileclient
File name :myfile
Hello worild……
-------------
/* server program */
# include
"unp.h"
/* UDP echo server
dg_echo function: */
void dg_echo(int sockfd,SA*pcliaddr,socklen_t clilen)
{
FILE*f;
int n,i=0,ch;
socklen_t len;
char mesg[MAXLINE];
for(;;){
len=clilen;
n=Recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
printf(" sending data.");
f=fopen("myfile","r");
while((ch=getc(f))!=-1)
mesg[i++]=ch;
mesg[i]=0; //init null
Sendto(sockfd,mesg,i,0,pcliaddr,len);
}
}
/*UDP echo server main
function:*/
#include"unp.h"
int main(int argc,char **argv[])
{
int sockfd;
struct sockaddr_in
servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
Bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
dg_echo(sockfd,(SA*)&cliaddr,sizeof(cliaddr));
}
$ cc udp_simple_file_server.c
wrapsock.c –o udpfileserver
/* execution */
$ ./
udpfileserver
Wating for client
request :
W8
Design
TCP concurrent server to convert a given text into upper case using
multiplexing system call select()
/* TCP
CLIENT with SELECT */
#include <stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include <errno.h>
#include
<string.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int ,
char *);
int main( int C, char
*V[] )
{
int sd,fd;
char c;
struct sockaddr_in
serveraddress;
char text[100];
int i=0;
sd = socket( AF_INET,
SOCK_STREAM, 0 );
if( sd < 0 ) {
perror(
"socket" );
exit( 1 );
}
if (V[1] == NULL ) {
printf ("PL specfiy
the server's IP Address \n");
exit(0);
}
if (V[2] == NULL ) {
printf ("PL specify
the server's Port No \n");
exit(0);
}
memset(
&serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family
= AF_INET;
serveraddress.sin_port =
htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr
= inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct
sockaddr*)&serveraddress,
sizeof(serveraddress))<0)
{
printf("Cannot
Connect to server");
exit(1);
}
printf("enter
sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';
sendstring(sd,text);
close(sd);
return 0;
}
/***********************************************************
* FUNCTION
NAME:sendstring
* DESCRIPTION: sends a
string over the socket .
* NOTES : No Error
Checking is done .
* RETURNS :void
**********************************************************/
void sendstring(
int sd, /*Socket
Descriptor*/
char *fname) /*Array
Containing the string */
/***********************************************************/
{ int n , byteswritten=0
, written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while
(byteswritten<n)
{
written=write(sd ,
buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("String : %s
sent to server \n",buffer);
}
/***********************************************************/
/*
Compiling and running
Client.
root@localhost
week7and8]# ./tcpclient 127.0.0.1 13153
enter sentence to end
enter #abcd#
String : abcd sent to
server
*/
/*TCP
SERVER_SELECT*/
#include <stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include <errno.h>
#include
<string.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include <sys/select.h>
#include
<sys/time.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include <fcntl.h>
#define MAXLINE 100
#define SERV_PORT 13153
int main(int argc, char
**argv)
{
int k, i, maxi, maxfd,
listenfd, connfd, sockfd;
int nready,
client[FD_SETSIZE];
ssize_t n;
fd_set rset, allset;
char
line[MAXLINE],buf[100];
socklen_t clilen;
struct sockaddr_in
cliaddr, servaddr;
listenfd =
socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0 )
{
perror("socket"
);
exit(1);
}
bzero(&servaddr,
sizeof(servaddr));
servaddr.sin_family =
AF_INET;
servaddr.sin_addr.s_addr
= htonl(INADDR_ANY);
servaddr.sin_port =
htons(SERV_PORT);
bind(listenfd, (struct
sockaddr *) &servaddr, sizeof(servaddr)); G. Narayanamma Institute of
Technology & Science.(for Women)– CSE Dept 6 Day Workshop on “Network
Programming and Simulators” 5th – 10th July 2010 - 30 -
listen(listenfd,5);
maxfd = listenfd; /*
initialize */
maxi = -1; /* index into
client[] array */
for (i = 0; i <
FD_SETSIZE; i++)
client[i] = -1; /* -1
indicates available entry */
FD_ZERO(&allset);
FD_SET(listenfd,
&allset);
/* end fig01 */
/* include fig02 */
for ( ; ; ) {
printf("Server:I am
waiting-----Start of Main Loop\n");
rset = allset; /*
structure assignment */
nready = select(maxfd+1,
&rset, NULL, NULL, NULL);
if (FD_ISSET(listenfd,
&rset)) {
/* new client connection
*/
clilen =
sizeof(cliaddr);
connfd =
accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
#ifdef NOTDEF
printf("new client:
%s, port %d\n",inet_ntop(AF_INET,
&cliaddr.sin_addr,
buf, NULL),ntohs(cliaddr.sin_port));
#endif
for (i = 0; i <
FD_SETSIZE; i++)
if (client[i] < 0) {
client[i] = connfd; /*
save descriptor */
break;
}
if (i == FD_SETSIZE)
{
printf("too many
clients");
exit(0);
}
FD_SET(connfd,
&allset);
/* add new descriptor to
set */
if (connfd > maxfd)
maxfd = connfd;
/* for select */
if (i > maxi)
maxi = i;
/* max index in client[]
array */
if (--nready <= 0)
continue;
/* no more readable
descriptors */
}
for (i = 0; i <=
maxi; i++) {
/* check all clients for
data */
if ( (sockfd =
client[i]) < 0)
continue;
if (FD_ISSET(sockfd,
&rset)) {
if ( (n = read(sockfd,
line, MAXLINE)) == 0) {
/*4connection closed by
client */
close(sockfd);
FD_CLR(sockfd,
&allset);
client[i] = -1;
} else
{
printf("\n output
at server\n");
for(k=0;line[k]!='\0';k++)
printf("%c",toupper(line[k]));
write(sockfd, line, n);
}
if (--nready <= 0)
break;
/* no more readable
descriptors */
}
}
}
}
/*
EXECUTION
AND RESULTS
Compiling
and running server.
root@localhost
week7and8]# cc tcpservselect01.c
[root@localhost
week7and8]# mv a.out tcpservselect1
[root@localhost
week7and8]# ./tcpservselect
[root@localhost week7and8]# ./tcpservselect1
Server:I am
waiting-----Start of Main Loop
Server:I am
waiting-----Start of Main Loop
output at server
A B C DServer:I am
waiting-----Start of Main Loop
output at server
A B C DServer:I am
waiting-----Start of Main Loop
Server:I am
waiting-----Start of Main Loop
*/
There are many compilation errors in week 8 program!
ReplyDelete