- 论坛徽章:
- 0
|
给你一个正确的serverside。
/* serverside.c - a simple server application */
/* Copyright 1984-2006 Wind River Systems, Inc. */
/*
modification history
--------------------
01a,12oct06,d_c written.
*/
/* include */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
/* globals */
/* message to be sent to client on a connect */
const char MESSAGE[] = "Hello, World!\n";
/* set the maximum length the queue of pending connections may grow to */
const int BACK_LOG = 5;
int main(int argc, char *argv[])
{
int serverSocket = 0;
int enablePortReuse = 0;
int serverPort = 0;
int status = 0;
int childPid = 0;
char hostname[80] = "";
struct hostent *hostPtr = NULL;
struct sockaddr_in serverName = { 0 };
struct linger linger = { 0 };
/* read & validata input parameters */
if (argc != 2)
{
fprintf(stderr, "Usage: serverside <port>\n");
exit(-1);
}
serverPort = atoi(argv[1]);
if (serverPort < 0)
{
fprintf(stderr, "serverside: strange serverPort value specified\n");
exit (-1);
}
if ( (serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 )
{
fprintf(stderr, "serverside: error calling socket()\n");
exit (-1);
}
/* setup and enable listening */
enablePortReuse = 1;
if ( (status = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
(const char *) &enablePortReuse, sizeof(enablePortReuse))) == -1)
{
fprintf(stderr, "serverside: warning, setting SO_REUSEADDR failed\n");
}
linger.l_onoff = 1;
linger.l_linger = 30;
if ( (status = setsockopt(serverSocket, SOL_SOCKET, SO_LINGER, (const char *) &linger,
sizeof(linger))) == -1)
{
fprintf(stderr, "serverside: warning, setting SO_LINGER failed\n");
}
if ( (status = gethostname(hostname, sizeof(hostname))) == -1 )
{
fprintf(stderr, "serverside: error calling gethostname()\n");
exit (-1);
}
if ( (hostPtr = gethostbyname(hostname)) == NULL )
{
fprintf(stderr, "serverside: error calling gethostbyname(%s)\n", hostname);
exit (-1);
}
(void) memset(&serverName, 0, sizeof(serverName));
(void) memcpy(&serverName.sin_addr, hostPtr->h_addr, hostPtr->h_length);
serverName.sin_family = AF_INET;
serverName.sin_port = htons(serverPort);
if ( (status = bind(serverSocket, (struct sockaddr *) &serverName, sizeof(serverName))) == -1 )
{
fprintf(stderr, "serverside: error calling bind()\n");
exit (-1);
}
if ( (status = listen(serverSocket, BACK_LOG)) == -1 )
{
fprintf(stderr, "serverside: error calling listen()\n");
exit (-1);
}
/* wait for connection */
while (1)
{
struct sockaddr_in clientName = { 0 };
int clientSocket = 0;
int clientLength = sizeof(clientName);
(void) memset(&clientName, 0, sizeof(clientName));
if ( (clientSocket = accept(serverSocket, (struct sockaddr *) &clientName,
&clientLength)) == -1 )
{
fprintf(stderr, "serverside: error calling accept()\n");
exit (-1);
}
/* for each connection accepted, start a new process. this way we can
accept multiple connections */
childPid = fork();
switch (childPid)
{
case -1:
fprintf(stderr, "serverside: error calling fork()\n");
exit (-1);
case 0:
/* the child process. this is where we reply to the
client's message */
close(serverSocket);
/* insert on_line() */ write(clientSocket, MESSAGE, strlen(MESSAGE));
close(clientSocket);
/* close this child process as task is done */
exit(0);
default:
/* the parent process */
close(clientSocket);
}
}
return 0;
} |
|