server program


 
Thread Tools Search this Thread
Top Forums Programming server program
# 1  
Old 03-12-2007
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 11:42 AM.. Reason: add code tags
# 2  
Old 03-12-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Test program running taking much more time on high end server T5440 than low end server T5220

Hi all, I have written the following program and run on both T5440 and T5220 on same OS version. I found that T5540 server takes more time than T5220. Please find below the details. test1.cpp #include <iostream> #include <pthread.h> using namespace std; #define NUM_OF_THREADS 20... (17 Replies)
Discussion started by: sanjay_singh85
17 Replies

2. UNIX and Linux Applications

Need a FTP SERVER Program FREE

I need a FTP SERVER program for using in ubuntu, Do you know any for free? ps: there will be no internet connection so, vsftp will not work. need a tool (like filezilla but filezilla has no server version for unix ubuntu) that has to be installation package to extract in it. (6 Replies)
Discussion started by: F@NTOM
6 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Short Program for Checking Server date

Hi Guys, Good morning! I have a file which looks something like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server3 Thu Jan 12 11:10:39 EET 2012 ------------------------------------------------... (3 Replies)
Discussion started by: rymnd_12345
3 Replies

5. UNIX for Dummies Questions & Answers

Program: How to FTP logs from a Server to Desktop

Hi guys, Good day! Anyone there could suggest on how can I create a program that will get (ftp) the logs I need from a remote Server (running in Linux) into my Desktop (running in Windows 7). For Perl program suggestions, FYI that I'm using Active Perl version. The reason why I need this one is... (2 Replies)
Discussion started by: rymnd_12345
2 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 - 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... (4 Replies)
Discussion started by: mathu
4 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. UNIX for Dummies Questions & Answers

How to know what program is install on Unix server?

I would like to know how to check what program is install on the Unix system? like the add/remove inside the control panel can show what program installed. (4 Replies)
Discussion started by: zp523444
4 Replies

10. IP Networking

Tcp Ip Send Receive Server Program

Requirements: A server program should read a file and send the message to the client . if the file is not there, then switch to the receive part of the same program and receive any messages from the socket. If no messages to receive then switch to send part of the program to... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question