Ash's Blog

TCP — server

Posted on: February 23, 2011

// TCP — server

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

int main()
{
int cont,create_socket,new_socket,addrlen,fd;
int bufsize = 1024;
char *buffer = malloc(bufsize);
char fname[256];
struct sockaddr_in address;

if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf(“The socket was created\n”);

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(10000);

printf(“\nworking”);

if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf(“Binding Socket\n”);
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);

if (new_socket > 0)
printf(“The Client %s is Connected…\n”,
inet_ntoa(address.sin_addr));
recv(new_socket,fname, 255,0);
printf(“A request for filename %s Received..\n”, fname);
if ((fd=open(fname, O_RDONLY))<0) cont=”read(fd,”>0) {
send(new_socket,buffer,cont,0);
}
printf(“Request Completed\n”);
close(new_socket);
return close(create_socket);
}

// tcp —- client

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

int main(int argc,char *argv[])
{
int cont,create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
char fname[256];
struct sockaddr_in address;

if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf(“The Socket was created\n”);
address.sin_family = AF_INET;
address.sin_port = htons(10000);
inet_pton(AF_INET,argv[1],&address.sin_addr);

if (connect(create_socket,(struct sockaddr *) &address,
sizeof(address)) == 0)
printf(“The connection was accepted with the server %s…\n”,
argv[1]);
printf(“Enter The Filename to Request : “); scanf(“%s”,fname);
send(create_socket, fname, sizeof(fname), 0);
printf(“Request Accepted… Receiving File…\n\n”);
printf(“The contents of file are…\n\n”);
while((cont=recv(create_socket, buffer, bufsize, 0))>0) {
write(1, buffer, cont);
}
printf(“\nEOF\n”);
return close(create_socket);
}

[root@localhost cn]# cc server.c
[root@localhost cn]# ./a.out
The socket was created

workingBinding Socket
The Client 127.0.0.1 is Connected…
A request for filename client.c Received..
Request Completed
[root@localhost cn]#

[root@localhost cn]# cc client.c
[root@localhost cn]# ./a.out 127.0.0.1
The Socket was created
The connection was accepted with the server 127.0.0.1…
Enter The Filename to Request : client.c
Request Accepted… Receiving File…

The contents of file are…

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

int main(int argc,char *argv[])
{
int cont,create_socket;
int bufsize = 1024;
char *buffer = malloc(bufsize);
char fname[256];
struct sockaddr_in address;

if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf(“The Socket was created\n”);
address.sin_family = AF_INET;
address.sin_port = htons(10000);
inet_pton(AF_INET,argv[1],&address.sin_addr);

if (connect(create_socket,(struct sockaddr *) &address,
sizeof(address)) == 0)
printf(“The connection was accepted with the server %s…\n”,
argv[1]);
printf(“Enter The Filename to Request : “); scanf(“%s”,fname);
send(create_socket, fname, sizeof(fname), 0);
printf(“Request Accepted… Receiving File…\n\n”);
printf(“The contents of file are…\n\n”);
while((cont=recv(create_socket, buffer, bufsize, 0))>0) {
write(1, buffer, cont);
}
printf(“\nEOF\n”);
return
EOF
[root@localhost cn]#

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow up !

Enter your email address to follow this blog and receive notifications of new posts by email.

iTweet :

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Where are you?

%d bloggers like this: