JBJones
05-24-2001, 07:12 PM
hi a couple of u might remember me from a little while ago. Ive tried some code for reading and writing an exsisting file through a socket to client from a server, but couldnt get it working.
For Those of you who have not seen my previouse message. Im trying to do a small prgram which replicates a copy function using a socket. I need the client to request a file name, i then need the server to open the file, read the 1024bytes of the file into a buffer, and then write the buffer to the socket. The client then reads the data from the socket and writes it to a new file. Oh yeah, its got to be low level c. I really need help with this, Im posting my socket which iv got working so you can all see what iv done and hopefully give you all a better idea of what im trying to do. PLEASE FEEL FREE TO COPY AND COMPILE IT I know it works and make alterations, you can e-mail me at "Jon.Parkin@BTInternet.com"
/************************************************** ******************************************
header file for socket client server example
idco.h
************************************************** ******************************************/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#define PORT 6996
static char buf[BUFSIZ];
*********************************************
/************************************************** ******************************************
Local network domain connection orientated client
idcoclient.c
compile with socket library -lsocket
************************************************** ******************************************/
#include"idco.h"
int main (int argc, char *argv[])
{
int orig_sock, /*original socket descriptor in server*/
len;
/*length of server address*/
struct sockaddr_in serv_adr; /*netwrok address of server*/
struct hostent *host; /*the host (server)*/
if (argc != 2) /*expect name of server on cmd line*/
{
fprintf(stderr, "usage is %s server\n", argv[0]); exit(1);
}
host = gethostbyname(argv[1]);
if (host == (struct hostent *)NULL) /*get host info*/
{
perror("gethostbyname");
exit(2);
}
memset (&serv_adr, 0, sizeof(serv_adr)); /*clear structure*/
serv_adr.sin_family = AF_INET; /*set address type*/
memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length); /*addr*/
serv_adr.sin_port = htons(PORT); /*use our port*/
if ((orig_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{ /*Socket*/
perror("generate error ");
exit(3);
}
if (connect(orig_sock,(struct sockaddr *)&serv_adr,sizeof(serv_adr))< 0)
{ /*connect*/
perror("connect error ");
exit(3);
}
do
{
write(fileno(stdout), "> ", 3); /*prompt user*/
/* if ((len = read(fileno(stdin), buf, BUFSIZ)) > 0)
{
write(orig_sock, buf, len); */ /*write to socket*/
if ((len = read(orig_sock, buf, len)) > 0)
{ /*If reurned*/
write(fileno(stdout), buf, len); /*Display it*/
}
/* }*/
}
while (buf[0] != '.');
close(orig_sock);
exit (0);
}
********************************************
/************************************************** ******************************************
Local network domain connection orientated server
idcoserv.c
compile with socket library -lsocket
************************************************** ******************************************/
#include"idco.h"
int main (void)
{
int orig_sock, /*original socket descriptor in server*/
new_sock, /*new socket description form connect*/
clnt_len; /*length of client address*/
struct sockaddr_in
clnt_adr, /*network addresses for client and server*/
serv_adr;
int len, i;
if((orig_sock = socket(AF_INET, SOCK_STREAM, 0)) <0) /*Bind*/
{
perror("generate error");
exit(1);
}
memset (&serv_adr, 0, sizeof(serv_adr)); /*clear structure*/
serv_adr.sin_family = AF_INET; /*set address type*/
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); /*and interface*/
serv_adr.sin_port = htons(PORT); /*use our port*/
if (bind(orig_sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr))<0)
{ /*Socket*/
perror("bind error");
close(orig_sock);
exit(2);
}
if (listen(orig_sock, 5) < 0)
{ /*Listen*/
perror("Listen error ");
close(orig_sock);
exit(3);
}
for(; ;)
{
clnt_len = sizeof(clnt_adr);
if ((new_sock = accept(orig_sock, (struct sockaddr *)&clnt_adr, &clnt_len)) < 0)
{ /*ACCEPT*/
perror("accept error ");
close(orig_sock);
exit(4);
}
if (fork() == 0) /*child*/
{
while ((len = read(new_sock, buf, BUFSIZ)) >0)
{ /*read from client*/
for (i=0; i< len; i++)
buf[i] = toupper(buf[i]); /*change to upper case*/
write(new_sock, buf, len); /*write it back*/
if (buf[0] == '.') /*if client finished*/
break; /*stop reading*/
}
close(new_sock);
exit (0);
}
else
{
close(new_sock);
}
}
return 0;
}
********************************************
If when this is copied into an editor it is not very readible please e-mail me and i will send you the files and compiled files.
For those who do not have a c compiler but are using Linux, remember that if you type the following line in the konsole in the appropriate directory you will be able to compile the files.
e.g. cc idcoserv.c -o idcoserv
to run the program then type at the konsole
e.g. ./idcoserv
to run the client in a seperate konsol window type
e.g. ./idcoclient localhost.
Thankyou for reading. Please remember i am in desperate need of help if you have expertise in this area they will be much much appriciated.
Sorry for spelling.
Jon.Parkin@BTinternet.com
For Those of you who have not seen my previouse message. Im trying to do a small prgram which replicates a copy function using a socket. I need the client to request a file name, i then need the server to open the file, read the 1024bytes of the file into a buffer, and then write the buffer to the socket. The client then reads the data from the socket and writes it to a new file. Oh yeah, its got to be low level c. I really need help with this, Im posting my socket which iv got working so you can all see what iv done and hopefully give you all a better idea of what im trying to do. PLEASE FEEL FREE TO COPY AND COMPILE IT I know it works and make alterations, you can e-mail me at "Jon.Parkin@BTInternet.com"
/************************************************** ******************************************
header file for socket client server example
idco.h
************************************************** ******************************************/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#define PORT 6996
static char buf[BUFSIZ];
*********************************************
/************************************************** ******************************************
Local network domain connection orientated client
idcoclient.c
compile with socket library -lsocket
************************************************** ******************************************/
#include"idco.h"
int main (int argc, char *argv[])
{
int orig_sock, /*original socket descriptor in server*/
len;
/*length of server address*/
struct sockaddr_in serv_adr; /*netwrok address of server*/
struct hostent *host; /*the host (server)*/
if (argc != 2) /*expect name of server on cmd line*/
{
fprintf(stderr, "usage is %s server\n", argv[0]); exit(1);
}
host = gethostbyname(argv[1]);
if (host == (struct hostent *)NULL) /*get host info*/
{
perror("gethostbyname");
exit(2);
}
memset (&serv_adr, 0, sizeof(serv_adr)); /*clear structure*/
serv_adr.sin_family = AF_INET; /*set address type*/
memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length); /*addr*/
serv_adr.sin_port = htons(PORT); /*use our port*/
if ((orig_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{ /*Socket*/
perror("generate error ");
exit(3);
}
if (connect(orig_sock,(struct sockaddr *)&serv_adr,sizeof(serv_adr))< 0)
{ /*connect*/
perror("connect error ");
exit(3);
}
do
{
write(fileno(stdout), "> ", 3); /*prompt user*/
/* if ((len = read(fileno(stdin), buf, BUFSIZ)) > 0)
{
write(orig_sock, buf, len); */ /*write to socket*/
if ((len = read(orig_sock, buf, len)) > 0)
{ /*If reurned*/
write(fileno(stdout), buf, len); /*Display it*/
}
/* }*/
}
while (buf[0] != '.');
close(orig_sock);
exit (0);
}
********************************************
/************************************************** ******************************************
Local network domain connection orientated server
idcoserv.c
compile with socket library -lsocket
************************************************** ******************************************/
#include"idco.h"
int main (void)
{
int orig_sock, /*original socket descriptor in server*/
new_sock, /*new socket description form connect*/
clnt_len; /*length of client address*/
struct sockaddr_in
clnt_adr, /*network addresses for client and server*/
serv_adr;
int len, i;
if((orig_sock = socket(AF_INET, SOCK_STREAM, 0)) <0) /*Bind*/
{
perror("generate error");
exit(1);
}
memset (&serv_adr, 0, sizeof(serv_adr)); /*clear structure*/
serv_adr.sin_family = AF_INET; /*set address type*/
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); /*and interface*/
serv_adr.sin_port = htons(PORT); /*use our port*/
if (bind(orig_sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr))<0)
{ /*Socket*/
perror("bind error");
close(orig_sock);
exit(2);
}
if (listen(orig_sock, 5) < 0)
{ /*Listen*/
perror("Listen error ");
close(orig_sock);
exit(3);
}
for(; ;)
{
clnt_len = sizeof(clnt_adr);
if ((new_sock = accept(orig_sock, (struct sockaddr *)&clnt_adr, &clnt_len)) < 0)
{ /*ACCEPT*/
perror("accept error ");
close(orig_sock);
exit(4);
}
if (fork() == 0) /*child*/
{
while ((len = read(new_sock, buf, BUFSIZ)) >0)
{ /*read from client*/
for (i=0; i< len; i++)
buf[i] = toupper(buf[i]); /*change to upper case*/
write(new_sock, buf, len); /*write it back*/
if (buf[0] == '.') /*if client finished*/
break; /*stop reading*/
}
close(new_sock);
exit (0);
}
else
{
close(new_sock);
}
}
return 0;
}
********************************************
If when this is copied into an editor it is not very readible please e-mail me and i will send you the files and compiled files.
For those who do not have a c compiler but are using Linux, remember that if you type the following line in the konsole in the appropriate directory you will be able to compile the files.
e.g. cc idcoserv.c -o idcoserv
to run the program then type at the konsole
e.g. ./idcoserv
to run the client in a seperate konsol window type
e.g. ./idcoclient localhost.
Thankyou for reading. Please remember i am in desperate need of help if you have expertise in this area they will be much much appriciated.
Sorry for spelling.
Jon.Parkin@BTinternet.com