pritam123456
asked on
Socket programming
Can any one help me? am trying to write a socket for unix. Upon connection with a browser as a client, it should display a web page on the browser. can any got any idea how to to that? The language i want to use is C.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>send(fd2,"Welcome to my server.n",22,0);
here you should be reading from html file and sending what you read
here you should be reading from html file and sending what you read
ASKER
and if i Put the code like this
send(fd2,index.html,22,0);
will this work. and is this the only thing that i have to change in the whole code????
send(fd2,index.html,22,0);
will this work. and is this the only thing that i have to change in the whole code????
No ... Read the file .. fread,fgets etc
Are you familiar with file I/O ?
Are you familiar with file I/O ?
ASKER
no am not quite familiar with this. can you help me with that. i already have a html how would i read it and send it to the browser??
Is this just an academic project or do you intend to write a real web server ?
some good theory about streams and examples for reading/writing files
http://www.cs.cf.ac.uk/Dave/C/node18.html#SECTION001800000000000000000
refer to man pages of
fopen
fread
fgets
fscanf
fwrite
fputs
fprintf
fclose
http://www.cs.cf.ac.uk/Dave/C/node18.html#SECTION001800000000000000000
refer to man pages of
fopen
fread
fgets
fscanf
fwrite
fputs
fprintf
fclose
ASKER
its just an accademic project. This input and output appear to be complicated. how should i start it.
just read the tutorial and write a couple of practice programs
hey sunny, he's my hero: http://www.ecst.csuchico.edu/~beej/guide/net/html/
I've even mirror'ed it, if you're in the uk this should be quicker:
http://www.keteracel.com/comframe.php?link=http://www.keteracel.com/community/beej/
I've even mirror'ed it, if you're in the uk this should be quicker:
http://www.keteracel.com/comframe.php?link=http://www.keteracel.com/community/beej/
interesting keteracel ... I learnt my entire socket programing from this tutorial ... never read a book ;o) ... I was quite impressed with it the first time ... Tutorials of this quality are a rarity
ASKER
the IO with Unix socket using C is so confusing. can anyone gave me a sample code of how to read from a text file or html file using C Coding??
ASKER
i have been able to modify some code. its can read the html file. but when i am using my web browser to connect to the server its still not working. it is not sending the index.html file. can some find the error for me please?
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <curses.h>
#include <sys/time.h>
#include <netdb.h>
#define MAXLINE 1000
#define SA struct sockaddr
#define DOCUMENT "index.html"
void
sigchld_handler(int s)
{
while (wait(NULL) > 0);
}
void
send_doc(int connfd)
{
FILE *fp;
char output[1000];
int len;
if((fp = fopen(DOCUMENT, "r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
fgets(output, 999, fp);
len = strlen(output);
if (sendall(connfd, output, &len) == -1) {
perror("sendall");
printf("We only sent %d bytes because of the error!\n", len);
}
}
}
int
sendall(int s, char *buf, int *len)
{
int total = 0;
int bytesleft = *len;
int n;
while (total < *len) {
n = send(s, buf + total, bytesleft, 0);
if (n == -1) {
break;
}
total += n;
bytesleft -= n;
}
*len = total;
return n == -1 ? -1 : 0;
}
int
main(void)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
struct sigaction sa;
int len, yes = 1, sin_size, nbytes;
char data[1000];
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((connfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(8080);
if (bind(listenfd, (struct sockaddr *) & servaddr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(listenfd, 10) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
for (;;) {
clilen = sizeof(cliaddr);
if ((connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) == -1) {
perror("accept");
exit(1);
}
printf("server: got connection from %s\n", inet_ntoa(cliaddr.sin_addr ));
if ((childpid = fork()) == 0) {
close(listenfd);
send_doc(connfd);
sleep(10);
exit(0);
}
close(connfd);
}
}
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <curses.h>
#include <sys/time.h>
#include <netdb.h>
#define MAXLINE 1000
#define SA struct sockaddr
#define DOCUMENT "index.html"
void
sigchld_handler(int s)
{
while (wait(NULL) > 0);
}
void
send_doc(int connfd)
{
FILE *fp;
char output[1000];
int len;
if((fp = fopen(DOCUMENT, "r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
fgets(output, 999, fp);
len = strlen(output);
if (sendall(connfd, output, &len) == -1) {
perror("sendall");
printf("We only sent %d bytes because of the error!\n", len);
}
}
}
int
sendall(int s, char *buf, int *len)
{
int total = 0;
int bytesleft = *len;
int n;
while (total < *len) {
n = send(s, buf + total, bytesleft, 0);
if (n == -1) {
break;
}
total += n;
bytesleft -= n;
}
*len = total;
return n == -1 ? -1 : 0;
}
int
main(void)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
struct sigaction sa;
int len, yes = 1, sin_size, nbytes;
char data[1000];
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((connfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(8080);
if (bind(listenfd, (struct sockaddr *) & servaddr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(listenfd, 10) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
for (;;) {
clilen = sizeof(cliaddr);
if ((connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) == -1) {
perror("accept");
exit(1);
}
printf("server: got connection from %s\n", inet_ntoa(cliaddr.sin_addr
if ((childpid = fork()) == 0) {
close(listenfd);
send_doc(connfd);
sleep(10);
exit(0);
}
close(connfd);
}
}
any error messages ? some diagnostic symptoms ?
ASKER
it is not working with the internet explorer. when i used another console application as client, i can get all the html coding but with internet explorer it is not working.
run the server on port 80 or tell internet explorer explicitly which port to connect to
http://my_server_ip:my_port
http://my_server_ip:my_port
ASKER
i have been trying it atleast 10 times with different port number including the port 80 but it is still not working.
ASKER
i have been running it exactly as above but it is not working have u tried it???
ASKER
can anyone help me. I have tried the above code its working perfectly. it is displaying index.html as default which is in the root directory. now i want to modify the code so that it can display any requested page stored in the root folder.? how will i do that??
ASKER
#define PORT 3550
main()
{
int fd, fd2;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ /* calls socket() */
printf("socket() errorn");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY; */
bzero(&(server.sin_zero),8
if(bind(fd,(struct sockaddr*)&server,sizeof(s
printf("bind() errorn");
exit(-1);
}
if(listen(fd,BACKLOG) == -1){ printf("listen() errorn");
exit(-1);
}
while(1){
sin_size=sizeof(struct sockaddr_in);
if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size))==-1)
printf("accept() errorn");
exit(-1);
}
printf("You got a connection from %sn",inet_ntoa(client.sin_
send(fd2,"Welcome to my server.n",22,0);
close(fd2);}
}