• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Programmers?? A little Homework help? (Networking)

EmperorIQ

Platinum Member
Hi,

I'm new to C and socket programming, I would post this in software forums, but no one hardly replies there since it isn't as popular as ATOT. I want to get a substring from an HTTP request message which is at first

GET /index.html HTTP/1.1

i got it down to /index.html with strtok(), but i can't seem to extract out that extra /, can anyone help me with that? how would i remove the first char in the string "/index.html"

Thanks in advance.
 
you could just create a new string with the first character missing

then you will have a string like "images/myImage.gif" that you can send to a system call to send.
 
how would i create a new string with the first character missing?

thanks for the reply johnjbruin, sorry though, I don't know enough about C to fullly understand what you're saying. Can we index a string in C? bah!
 
Originally posted by: EmperorIQ
how would i create a new string with the first character missing?

thanks for the reply johnjbruin, sorry though, I don't know enough about C to fullly understand what you're saying. Can we index a string in C? bah!

yeah
do a malloc for space for a new string
then do a char by char copy - skip the first one

i love java for this reason - god loves clone()
 
#include <stdio.h>

int main( void ) {

char * origString = "abc";
char * newString;

origString++;
newString = origString;

printf("%s", newString);


return 0;
}



newString is now "bc"

hurray!
 
#include <stdio.h>

int main( void ) {

char * origString = "abc";
char * newString = (origString + 1);

printf("%s", newString);


return 0;
}



there you go. this preserves origString.

If you don't care about origString, then you can just do origString++ and not even make newString.
 
thanks guys, i got it to work now, i have another question about socket programming, my instructor told me to write it so that it'll pass in a response message to the browser giving it content type and content length, but my webserver program seems to work out fine without the me sending a response message.

1) do socket functions already send in a response for you?
2) i've been trying to send in a response with send() is that the correct format? or should i use write()? or are they the same? My TA told me that write is the same as send and read is the same as recv, but it doesn't look like it would be. Thanks a lot for all of your help!
 
Here is some sample code that should help a TON.

/*-----------------------------------------------------------------------------
* Program: TCPserver
*
* Purpose: set up an concurrent TCP server waiting for message
* from client and sending back reply
*
* Usage: TCPserver
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>

#define BACKLOG 5 /* # of request we're willing to queue */
#define MAXHOSTNAME 32 /* maximum host name length we tolerate */
#define PORTNUM 8888 /* fixed port number to listen to */

err_sys(char * msg) {
perror(msg);
exit(-1);
}


main(int argc,char **argv) {
int s,t; /* socket descriptors */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr_in clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char localhost[MAXHOSTNAME+1]; /* local host name as character string*/
int cli_len, childpid;

/*
* Deal with the command line arguments
*/

/*
* get our own host information
*/
gethostname(localhost, MAXHOSTNAME);
if ((hp = gethostbyname(localhost)) == NULL) {
fprintf(stderr, "%s: cannot get local host info?\n", argv[0]);
exit(1);
}

/*
* Put the port number and our address info into the socket structure
*/
bzero((char*) &amp;srv_addr, sizeof(srv_addr));
bcopy((char *)hp->h_addr, (char *)&amp;srv_addr.sin_addr, hp->h_length);
srv_addr.sin_family = hp->h_addrtype;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Server host: %s, port: %d\n\n", localhost, srv_addr.sin_port);

/*
* Allocate an open socket for incoming connections
*/
if ((s = socket(srv_addr.sin_family, SOCK_STREAM,0)) <0)
err_sys("socket");

/*
* Bind the socket to the service port
*/
if (bind(s, (struct sockaddr *)&amp;srv_addr, sizeof srv_addr) <0)
err_sys("bind");

/*
* Set maximum connections we will fall behind
*/
if (listen(s, BACKLOG)<0)
err_sys("listen");

/*
* Go into an infinite loop waiting for new connections
*/
while (1) {
/*
* We hang in accept() while waiting for new clients
*/
if ((t = accept(s, (struct sockaddr *)&amp;clt_addr, &amp;cli_len))<0)
err_sys("accept");

switch (childpid = fork()) {
case -1: err_sys("fork");
case 0:
close(s);
/*
* the child performs the actual service
*/
service(t);
close(t);
exit(0);
default:
close(t);
}

if (waitpid(0, NULL, WNOHANG) == -1) perror("waitpid");
}
}

service(int sock) {
char buf[BUFSIZ + 1];
int n;

/*
* receive client's request
*/
if ((n = read(sock, buf, sizeof(buf)-1)) == -1)
perror("read");
buf[n] = '\0';
fprintf(stdout, "the client requests:\n%s\n", buf);

/*
* send back reply
*/
if (write(sock, buf, strlen(buf)) == -1)
perror("write");
}
/*-----------------------------------------------------------------------------
* Program: TCPclient.c
*
* Purpose: send message to server by TCP and get feedback
*
* Usage: TCPclient server_IP_address
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>

#define PORTNUM 8888 /* fixed port number to connect to */

err_sys(char * msg) {
perror(msg);
exit(-1);
}


main(int argc, char **argv) {
int s; /* socket descriptor */
struct sockaddr_in srv_addr; /* server socket address */
struct hostent * hp; /* result of host name lookup */
char buf[BUFSIZ+1]; /* buffer to read service information */
int n;

/*
* Deal with the command line arguments
*/
if (argc<2) {
fprintf(stderr,"Usrv_addrge: %s host\n", argv[0]);
exit(1);
}

/*
* Put the server's address and address type into socket structure
*/
bzero((char*) &amp;srv_addr, sizeof(srv_addr));
if ((srv_addr.sin_addr.s_addr = inet_addr(argv[1])) == -1) {
fprintf(stderr, "%s: %s: Invalid host IP address\n", argv[0], argv[1]);
exit(1);
}
srv_addr.sin_family=AF_INET;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Connect to host: %s, port: %d\n\n", argv[1], srv_addr.sin_port);
fflush(stdout);

/*
* Allocate an open socket
*/
if ((s=socket(srv_addr.sin_family, SOCK_STREAM,0))<0)
err_sys("socket");

/*
* Connect to the remote server
*/
if (connect(s, (struct sockaddr *)&amp;srv_addr, sizeof srv_addr)<0)
err_sys("connect");

/*
* Send the request and get reply
*/
sprintf(buf, "%d\n", getpid());

if (write(s, buf, strlen(buf)) == -1)
perror("write");

if ((n = read(s, buf, sizeof(buf)-1)) == -1)
perror("read");
buf[n] = '\0';

fprintf(stdout, "the server replies:\n%s\n", buf);
close(s);
}

/*-----------------------------------------------------------------------------
* Program: UDPserver
*
* Purpose: set up an iterative UDP server waiting for message
* from client and sending back reply
*
* Usage: UDPserver
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>

#define MAXHOSTNAME 32 /* maximum host name length we tolerate */
#define PORTNUM 8888 /* fixed port number for communication */

err_sys(char * msg) {
perror(msg);
exit(-1);
}


main(int argc,char **argv) {
int s; /* socket descriptors */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char localhost[MAXHOSTNAME+1]; /* local host name as character string*/
char buf[BUFSIZ+1];
int n, clt_len;

/*
* Deal with the command line arguments
*/


/*
* get our own host information
*/
gethostname(localhost, MAXHOSTNAME);
if ((hp = gethostbyname(localhost)) == NULL) {
fprintf(stderr, "%s: cannot get local host info?\n", argv[0]);
exit(1);
}

/*
* Put the port number and our address info into the socket structure
*/
bzero((char*) &amp;srv_addr, sizeof(srv_addr));
bcopy((char *)hp->h_addr, (char *)&amp;srv_addr.sin_addr, hp->h_length);
srv_addr.sin_family = hp->h_addrtype;
srv_addr.sin_port = htons(PORTNUM);
fprintf(stdout, "Server host: %s, port: %d\n\n", localhost, srv_addr.sin_port);

/*
* Allocate an open socket and bind
*/
if ((s = socket(srv_addr.sin_family, SOCK_DGRAM,0)) <0)
err_sys("socket");

if (bind(s, (struct sockaddr *)&amp;srv_addr, sizeof srv_addr) <0)
err_sys("bind");

/*
* Go into an infinite loop waiting for new clients
*/
while (1) {

/*
* receive request and client socket address
*/
if ((n = recvfrom(s, buf, sizeof(buf)-1, 0, &amp;clt_addr, &amp;clt_len)) <0)
perror("recvfrom");
buf[n] = '\0';
fprintf(stdout, "the client requests:\n%s\n", buf);

/*
* send back reply
*/
if (sendto(s, buf, strlen(buf), 0, &amp;clt_addr, clt_len) < 0)
perror("sendto");
}
}

/*-----------------------------------------------------------------------------
* Program: UDPclient.c
*
* Purpose: send message to server by UDP and get feedback
*
* Usage: UDPclient server_IP_address
*
* Author: Tao Yang
*
*-----------------------------------------------------------------------------
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>

#define PORTNUM 8888 /* fixed port number for communication */

err_sys(char * msg) {
perror(msg);
exit(-1);
}

main(int argc,char **argv) {
int s; /* socket descriptor */
struct sockaddr_in srv_addr; /* server socket address */
struct sockaddr_in clt_addr; /* client socket address */
struct hostent * hp; /* result of host name lookup */
char buf[BUFSIZ+1]; /* buffer to read service information */
int n, srv_addr_len;

/*
* Deal with the command line arguments
*/
if (argc<2) {
fprintf(stderr,"Usrv_addrge: %s host\n", argv[0]);
exit(1);
}

/*
* Put the server's address and address type into socket structure
*/
bzero((char*) &amp;srv_addr, sizeof(srv_addr));
if ((srv_addr.sin_addr.s_addr = inet_addr(argv[1])) == -1) {
fprintf(stderr, "%s: %s: Invalid host IP address\n", argv[0], argv[1]);
exit(1);
}
srv_addr.sin_family=AF_INET;
srv_addr.sin_port = htons(PORTNUM);

/*
* Put my own address and address type into socket structure
*/
bzero((char *) &amp;clt_addr, sizeof(clt_addr));
clt_addr.sin_family = AF_INET;
clt_addr.sin_addr.s_addr = htonl (INADDR_ANY);
clt_addr.sin_port = htons(0);

/*
* Allocate an open socket and bind
*/
if ((s=socket(srv_addr.sin_family, SOCK_DGRAM,0))<0)
err_sys("socket");

if (bind(s, (struct sockaddr *)&amp;clt_addr, sizeof(clt_addr)) <0)
err_sys("bind");

/*
* Send the request and get reply
*/
sprintf(buf, "%d\n", getpid());

if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&amp;srv_addr, sizeof srv_addr) == -1)
err_sys("sendto");

if ((n = recvfrom(s, buf, sizeof(buf)-1, 0, (struct sockaddr *)&amp;srv_addr, &amp;srv_addr_len)) == -1 )
err_sys("recvfrom");
buf[n] = '\0';

fprintf(stdout, "the server replies:\n%s\n", buf);
close(s);
}
 
Back
Top