The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Server client program pip3r High Level Programming 9 04-16-2008 10:15 PM
Client and Server program gen by Makefile wongalan48 High Level Programming 0 03-05-2007 10:09 AM
Client - server program mathu High Level Programming 4 09-17-2006 09:56 AM
Chat client-server program powermind High Level Programming 1 09-04-2006 08:19 AM
running dos program from unix server rkap UNIX for Dummies Questions & Answers 1 04-06-2005 07:48 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 21
server program

hi all

i would like to write a simple server program to say "helloworld" when server is up
and say "bye world" when server is down

how can handle this

i tried some
Code:
#include <stdio.h>		/* Basic I/O routines 		*/
#include <sys/types.h>		/* standard system types	*/
#include <netinet/in.h>		/* Internet address structures  */
#include <sys/socket.h>		/* socket interface functions   */
#include <netdb.h>		/* host to IP resolution 	*/

#define	PORT		"5050"	/* port of "hello world" server */
#define	LINE	"hello world"	/* what to say to our clients  */

void main()
{
    int			rc;       /* system calls return value storage  */
    int			s;        /* socket descriptor                  */
    int			cs;       /* new connection's socket descriptor */
    struct sockaddr_in	sa;       /* Internet address struct            */
    struct sockaddr_in	csa;      /* client's address struct            */
    int               	size_csa; /* size of client's address struct    */

    /* initiate machine's Internet address structure */
    /* first clear out the struct, to avoid garbage  */
    memset(&sa, 0, sizeof(sa));
    /* Using Internet address family */
    sa.sin_family = AF_INET;
    /* copy port number in network byte order */
    sa.sin_port = htons(PORT);
    /* we will accept cnnections coming through any IP	*/
    /* address that belongs to our host, using the	*/
    /* INADDR_ANY wild-card.				*/
    sa.sin_addr.s_addr = INADDR_ANY;

    /* allocate a free socket                 */
    /* Internet address family, Stream socket */
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
	perror("socket: allocation failed");
    }

    /* bind the socket to the newly formed address */
    rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));

    /* check there was no error */
    if (rc) {
	perror("bind");
    }

    /* ask the system to listen for incoming connections	*/
    /* to the address we just bound. specify that up to		*/
    /* 5 pending connection requests will be queued by the	*/
    /* system, if we are not directly awaiting them using	*/
    /* the accept() system cal, when they arrive.		*/
    rc = listen(s, 5);

    /* check there was no error */
    if (rc) {
	perror("listen");
    }

    /* remember size for later usage */
    size_csa = sizeof(csa);
    /* enter an accept-write-close infinite loop */
    while (1) {
       	/* the accept() system call will wait for a	*/
       	/* connection, and when one is established, a	*/
       	/* new socket will be created to form it, and	*/
       	/* the csa variable will hold the address	*/
       	/* of the Client that just connected to us.	*/
       	/* the old socket, s, will still be available	*/
       	/* for future accept() statements.		*/
       	cs = accept(s, (struct sockaddr *)&csa, &size_csa);

       	/* check for errors. if any, enter accept mode again */
       	if (cs < 0)
    	    continue;

	/* ok, we got a new connection. do the job... */
	write(cs, LINE, sizeof(LINE));

	/* now close the connection */
	close(cs);
    }
}
i saved it as server.c and copmpiled like
cc server.c -o server
./server

it not showing any message from server

can you please help me

Last edited by blowtorch; 03-12-2007 at 07:42 AM. Reason: add code tags
Reply With Quote
Forum Sponsor
  #2  
Old 03-12-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
Quote:
cs = accept(s, (struct sockaddr *)&csa, &size_csa);

/* check for errors. if any, enter accept mode again */
if (cs < 0)
continue;

/* ok, we got a new connection. do the job... */
write(cs, LINE, sizeof(LINE));
Have you started any client to communicate with the server?

If not,

you have the answer,

server blocks on accept, listening for incoming requests from the client,

only if that condition is satisfied, a valid accept descriptor is available to write message to the specific client
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0