Client - server program


 
Thread Tools Search this Thread
Top Forums Programming Client - server program
# 1  
Old 09-16-2006
Client - server program

i came acors this coding when surfin the net.this code works perfectly.but as i am new to this socket programming i need sm coments quoted on it or explanation regarding this source code.
i have prb understanding the server.c i have posted it below
can u guys help me !!!!
cheerZ


The server.c
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#define MAX_CLIENTS 64

struct sockaddr_in servaddr;
int addr_len;
struct sockaddr addr;
struct sockaddr_in *ptr;
int cl_addr_len;
struct sockaddr_in cl_addr;
char in_buf[100], out_buf[100];
char names [10][30];
char message[150];
int num_names = 0;

	typedef struct cl_info
		{
			char cl_id[30];
			int cl_status;
		}cl_info;

	cl_info clients[MAX_CLIENTS];

	int main(int argc, char **argv)
		{
			int newsockfd;
			int sockfd;
			int nbytes;
			int max_fd;
			int fd;
			short func_num;

			fd_set test_set, ready_set;



	sockfd = socket(AF_INET, SOCK_STREAM, 0); 
	servaddr.sin_family - AF_INET;
	servaddr.sin_port = 0;				 /*choose an unused port at random */
	servaddr.sin_addr.s_addr = INADDR_ANY;	 	/* auto fill with my IP address */
	bind(sockfd,(struct sockaddr*) &servaddr, sizeof(servaddr));

	addr_len = sizeof(addr);
	getsockname(sockfd, &addr, &addr_len);
	ptr = (struct sockaddr_in *)&addr;
	printf("\nServer on port# %d\n", ptr->sin_port);

	listen(sockfd, 5);
	max_fd = sockfd; 
	FD_ZERO(&test_set); /*clear the listening socket */

	FD_SET(sockfd, &test_set);

	

	while(1)
		{
			memcpy(&ready_set, &test_set, sizeof(test_set));
			select(max_fd+1, &ready_set, NULL, NULL, NULL);
			
			if(FD_ISSET(sockfd, &ready_set)) /*if listening sock has data its a call from new client */
				{
					/*create a new socket */

					cl_addr_len = sizeof(cl_addr);
					newsockfd = accept(sockfd, (struct sockaddr *)&cl_addr, &cl_addr_len);

					printf("\nA new connection has been made to socket %d\n", newsockfd);
					FD_SET(newsockfd, &test_set); /* add new socket to list */
					if(newsockfd > max_fd)
						max_fd = newsockfd; /*update max_fd with new socket file discriptor */
				} /*end if*/


			for(fd=0; fd <= max_fd; fd++) /*loop through connected socket file discriptors */
{
if((fd !=sockfd) && FD_ISSET(fd, &ready_set))/*bypass listening sock if sock has data*/
if(process_call(fd, max_fd) == 0) /*if socket is closed */
{
printf("\nThe listening socket is now closed\n");
close(newsockfd);
printf("\nClient on socket %d has left the room\n", sockfd);
FD_CLR(fd, &test_set); /*remove socket from list */
/*decrement user count */
} /*end if */
} /* end for loop */
} /*end while loop */
return 0;
} /*end main */
int add_name(int sockfd)
{
int nbytes;
short name_len;
short n_name_len;

printf("\nAdding name .....\n");
nbytes = read(sockfd, (char *)&name_len, sizeof(short));
name_len = ntohs(name_len); /*convert name_len from network to host short */
memset(clients[sockfd].cl_id, 0, 30); /*set all bytes to zero */
nbytes = read(sockfd, clients[sockfd].cl_id, name_len);
num_names ++; /*increment user count */
printf("\n%s has joined the chat room\n", clients[sockfd].cl_id);

/*need to broadcast user has joined plus user id to connected clients here */

} /*end add_name function */
int how_many(int sockfd, int max)
{
int sock;
short user_len;
char username[30];

for(sock = 4; sock <= max; sock++)
{
strcpy(username, clients[sock].cl_id);

user_len = htons(strlen(username));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, username, strlen(username));
send_user_list(sockfd, username);
}
} /*end how_many function */
int send_user_list(int sockfd, char *users)
{
short msg_type;
short user_len;

msg_type = htons(2);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));

user_len = htons(strlen(users));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, users, strlen(users));
}
int find_name(int sockfd, int max)
{
char search_name[30];
int sock;
int found = 0;
short len, name_len;
char sender[30];
short sender_len;
char msg[150];
short msg_len;

memset(search_name, 0, 30); /*set all bytes to zero */
printf("\nReceiving userid......\n");
read(sockfd, (char *)&name_len, sizeof(short));
len = ntohs(name_len);
read(sockfd, search_name, len);
read(sockfd, &msg_len, sizeof(short));
msg_len = ntohs(msg_len);
read(sockfd, msg, msg_len);
msg[msg_len] = '\0';

strcpy(sender, clients[sockfd].cl_id);

for(sock = 4; sock <= max; sock++)
{
printf(" %d is held in max\n", max);
printf("\ncomparing search request for %s with %s username\n",
search_name, clients[sock].cl_id);
if(strcmp(search_name, clients[sock].cl_id) ==0)
{
printf("\nuserid found\n");
found = 1;
send_async_msg(sock, msg,sender);
}/*end if */
} /*end for loop */

if(! found)
{
printf("\nuserid not found\n");
} /*end if */
}/*end find_name function */
int process_call(int newsockfd, int max_fd)
{
int nbytes;
short func_num;

nbytes = read(newsockfd, (char *)&func_num, sizeof(short));
if(nbytes ==0)
return 0;
if (nbytes != sizeof(short))
{
perror("\nUnknown menu option!\n");
exit(1);
} /* end if */

func_num = ntohs(func_num);
switch(func_num)
{
case(1):
add_name(newsockfd);
break;
case(2):
how_many(newsockfd, max_fd);
break;
case(3):
find_name(newsockfd, max_fd);
break;
case(4):
send_global_msg(newsockfd, max_fd);
break;
case(5):
printf("\nShutting down Server...\n");
exit(0);
default:
printf("\nUnknown function number!\n");
break;
} /*end switch */
} /*end process_call function */
int send_async_msg(int sockfd, char * msg, char *sender)
{
short msg_type;
short msg_len;
short sender_len;

msg_type = htons(1);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));
msg_len = htons(strlen(msg));
write(sockfd, (char *)&msg_len, sizeof(short));
write(sockfd, msg, strlen(msg));

sender_len = htons(strlen(sender));
write(sockfd, (char *)&sender_len, sizeof(short));
write(sockfd, sender, strlen(sender));
}/* end of send_async_msg function */
int send_global_msg(int sockfd, int max_fd)
{
int nbytes;
int sock;
short len;
short msg_type;
char msg[150];
char sender[30];
short sender_len;

strcpy(sender, clients[sockfd].cl_id);

msg_type = 1;
memset(in_buf, 0, sizeof(in_buf)); /*set all to zero */
nbytes = read(sockfd, &len, sizeof(short));
len = ntohs(len);
read(sockfd, msg, len);
message[len] = '\0';
for(sock = 4; sock <= max_fd; sock++)
{
send_async_msg(sock, msg, sender);
} /*end for loop */
} /* end of send_global_msg function */


Last edited by reborg; 09-16-2006 at 04:51 PM..
# 2  
Old 09-16-2006
there had been so many posts for network programming starters

one such is beej's guide to n/w prg

you can search the forum for more
# 3  
Old 09-16-2006
please read my post

Quote:
Originally Posted by matrixmadhan
there had been so many posts for network programming starters

one such is beej's guide to n/w prg

you can search the forum for more
will you please answer my question please that i have posted 2 hours ago.

Thank you.
# 4  
Old 09-17-2006
what is the post that you are referring to ?
is that from some other thread ??? Smilie Smilie Smilie
# 5  
Old 09-17-2006
Hello

Quote:
Originally Posted by matrixmadhan
what is the post that you are referring to ?
is that from some other thread ??? Smilie Smilie Smilie

Hello,

thank you. You all ready gave me answer about the extracting string and then printing it out. I thought that noone saw it.

At future, I will wait for a long time, before asking again. My question was how to extract a specific string from a file with sscanf( ) and then printing it out.

I got the answer. But now I wonder whether it is possible to extract more than one string from more than one line from a file. Say, we have a file,

cat /proc/cpuinfo

processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz

stepping : 9
cpu MHz : 2591.654
cache size : 128 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse

And I want to extract

vendor_id : GenuineIntel
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz

I know that it is possible by wrting the following code three times in this case as we are extracting three strings here.

if(NULL == (fp = fopen(argv[1], "rt"))){
printf("Can't open %s\n", argv[1]);
exit(1);
}
for(;fgets(buf, 255, fp)Smilie{
if(buf == strstr(buf, "model name")){
if(NULL != (p = strchr(buf, ':')))
printf("%s\n", p + 1);
break;
}

But is there a way, for which I can write the code once but can extract some specific strings and print them out at once one after the other.

And also, is there a way, so that in one C file I can open mutiple files by command line argument, one after the other. Say, after I am done with printing vendor_id, model type, and model name from cpuinfo file from /proc directory, I open the stat file from the /proc directory and start extracting some specific strings and printing them again. Is that a very compilcated way or much simple. Please let me know how to do that if you know.

And also, when we extract a string, say, cpu family : 15, 15 is a type string. Say, I want to convert string 15 to an integer; so, I use atoi(), but to print it, I have to do the following right?

int number = atoi(p);
printf("The integer value is: %d\n", number);

But well I cannot print the integer value. So, I think I am missing something.

Whatever help I can get, I will be very thankful.
Thank you. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SOAP Client server program

Hi, I have taken the below code from Quick Start with SOAP - Perl.com and modified to my requirement.Server program runs without error.I have kept Demo.pm under /usr/local/apache2/cgi-bin directory.When I run the client program I am not getting any output.Whether the client program should be... (1 Reply)
Discussion started by: liyakathali
1 Replies

2. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

3. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

4. Programming

SFTP client program

can u help me? i need the program code in C to perform Simple File Transfer in linux.in this forum i found the server program,inw i need the client program ASAP. Thanx. (1 Reply)
Discussion started by: harshi
1 Replies

5. Programming

How to program a telnet client?

Hi, Experts: I have programmed a simple telnet client in sco unix 5.0.5, the client has passed throught the initial option negotiation, but I can't receive login prompt from the server. please help me. (8 Replies)
Discussion started by: thinker130
8 Replies

6. Programming

Server client program

hi guys, I need the code for a server client registration form.The server must ask for authentication .Then the client would send in data. This is stored in a file .The server sends back a receipt to the client as part of the payment done. plz can some 1 get me the code... (9 Replies)
Discussion started by: pip3r
9 Replies

7. Programming

Client and Server program gen by Makefile

I created a "ebanking.x" file and run it as " rpcgen -a ebaning.x" It gen a few of files to me which is - "ebanking.h", "ebanking_server.c", "ebanking_svc.c", "ebanking_client.c", "ebanking_clnt.c", "ebanking_xdr.c" and "Makefile" The content of "ebanking.x" : struct bankargs { ... (0 Replies)
Discussion started by: wongalan48
0 Replies

8. Programming

Chat client-server program

Good day everyone, I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port. This is my client program. I'd not know why it just sends and receives msg from server once, then it stops. Is... (1 Reply)
Discussion started by: powermind
1 Replies

9. Programming

program to transfer a file from client machine to server

1. we have a client & server ,and TFTP is running on the server. 2.we have 3 files a.exe,b.exe,c.exe in the client machine....we need to transfer all the 3 files to the server and store it into a DIR... 3.then we need to check in the server whetehr all the three files are sucessfully... (3 Replies)
Discussion started by: nathgopi214
3 Replies

10. IP Networking

i want a UDP Client receiving program

Hi I want a UDP client receiving program. Here is the scenario. The client has to listen to the UDP server and has to acknowledge back on receiving data from the server. Can anyone help me out. regards, Nirmala (1 Reply)
Discussion started by: Nirmala
1 Replies
Login or Register to Ask a Question