-
Server with browser
I have coded a server program which interacts with my client, but it doesnt interact with a browser, what could be the problem?
here is the
server.c:
Code:
#include <netinet/in.h>
#include <arpa/inet.h>
main(int argc, char *argv[])
{
int sockfd, newsockfd ; /* Socket descriptors */
int clilen;
struct sockaddr_in cli_addr, serv_addr;
int port;
int i, readsocks;
char buf[100]; /* We will use this buffer for communication */
if(argc != 2) {
printf("\nEnter port number only in the command line argument\n");
exit(0);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Cannot create socket\n");
exit(0);
}
port = atoi(argv[1]);
printf("\nport is %d\n", port);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = port;
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
printf("Unable to bind local address\n");
exit(0);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,
&clilen);
printf("wow Some client is connected\n\n");
close(newsockfd);
}
I am using "http://127.0.0.1:8080/" in the address bar of the mozilla firefox.
am running the server program on the same machine on the port 8080.
actually the server should print:
Code:
wow Some client is connected
but it does nothing..
-
You've got a connection, but you don't send anything down it - what were you expecting to happen?
-
if i get a connection.. the server shoul print the line.. it is not printing that means the server has not get any connection..
-
System Calls or Bust
This suggests you need htons() on the port value.
Also, get Wireshark · Go deep. so you can see what is really happening "on the wire".
-
Port 8080 is a secure SSL port. The browser is probably trying to make a secured connection and gives up when it doesn't get the correct acknowledgements. Try it on port 80 which is the general HTTP port.
OR... use a different port altogether...
-
Port 8080 is not "a secure SSL port". 443 is the standard HTTPS port, but any port can actually serve as a secure port if the server is so configured.
To the OP: printf writes to standard output, not to the connected socket. You need to use send(), not printf().
-
I have changed the code to :
Code:
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
still not working.
The client program now also is unable to connect to the server:
here is the client program.
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
main(int argc, char *argv[])
{
int sockfd ;
struct sockaddr_in serv_addr;
if(argc != 3) {
printf("\nEnter SERVER_IP & PORT_NUMBER only in order as command line argument\n");
exit(0);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Unable to create socket\n");
exit(0);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = atoi(argv[2]);
if ((connect(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr))) < 0) {
printf("Unable to connect to server\n");
exit(0);
}
}
I think any port number can be used if it can be bind by the server program.
I want to connect this client program too, and make the server work with browser too.
I am not using send but write just to check weather there is any connection has been established or not as accept() call blocks the process till there is any connection made.