UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Socket code prob - i think.

#1 2008-11-22 01:42 PM

notvgoodatc
Member
Registered: 2008-11-22
Posts: 2

Re: Socket code prob - i think.

Hi, sorry not sure where to post as not sure what the problem is.

I have some C code which sets up a very simple TCP socket server, the socket basically waits for someone to connect and then returns a default response to the browser. I wanted to add some code using the MySQL API to query a mysql database and also return the result of the query to the same browser session.

However what happens is the default response returns to the browser and the query from the mysql api gets returns to the terminal that I am running the C program from. I think I need to somehow change the printf statement to similar 'write' one which returns the message to the browser, unfortunately I am not sure how to move forward, and would appreciate some friendly advice. Thanks in advance.

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/include/mysql/mysql.h>

void error(char *msg)
{
	perror(msg);
	exit(1);
}

int main(int argc, char *argv[])
{
	int sockfd, newsockfd, portno, clilen;
	char buffer[256];
	struct sockaddr_in serv_addr, cli_addr;
	int n;
	if (argc < 2) {
		fprintf(stderr,"ERROR, no port provided\n");
		exit(1);
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) 
		error("ERROR opening socket");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = atoi(argv[1]);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);
	if (bind(sockfd, (struct sockaddr *) &serv_addr,
			*sizeof(serv_addr)) < 0) 
		error("ERROR on binding");
	listen(sockfd,5);
while(1) {
	clilen = sizeof(cli_addr);
	newsockfd = accept(sockfd, 
				(struct sockaddr *) &cli_addr, 
				&clilen);
	if (newsockfd < 0) 
		error("ERROR on accept");
	bzero(buffer,256);
	n = read(newsockfd,buffer,255);
	if (n < 0) error("ERROR reading from socket");
	printf("Here is the message: %s\n",buffer);
	n = write(newsockfd,"yep,I got your message",22);
	if (n < 0) error("ERROR writing to socket");
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	
	char *server = "x.x.x.x";
	char *user = "xxxx";
	char *password = "xxxx";
	char *database = "xxxx";
	conn = mysql_init(NULL);
	
	if(!mysql_real_connect(conn, server,
		user,password, database, 0, NULL, 0)) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(0);
	}
	if (mysql_query(conn, "SELECT name from animals limit 1")) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(0);
	}
	res = mysql_use_result(conn);
	while ((row = mysql_fetch_row(res)) != NULL){
		printf("%s %s\n", row[1], row[2]);
		mysql_free_result(res);
		mysql_close(conn);
	}
	close(newsockfd);
	}
}

Offline

#2 2008-11-22 06:34 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Socket code prob - i think.

Hi there,

If I understand your question correctly, you basically want the text output that is going to stdout when you call printf() to be sent to your client's socket instead.  If that's the case (and assuming your are running on a Unix/POSIX style OS, i.e. not Windows), you can use the dup2() call to make your program's stdout file descriptor be the same as your client socket... after that, everything you printf() will be sent to the client instead of to the local terminal.  I.e.:

dup2(newsockfd, fileno(stdout));

-Jeremy

Offline

#3 2008-11-22 07:58 PM

notvgoodatc
Member
Registered: 2008-11-22
Posts: 2

Re: Socket code prob - i think.

thats great thanks so much, I will give that a try :-)

Offline

  • Index
  • » C
  • » Socket code prob - i think.

Board footer

Powered by FluxBB