📄 servertest.c
字号:
#include <socket.h>
#include <errnoLib.h>
#include <usrLib.h>
#include <string.h>
#include <stdio.h>
#include "in.h"
#include "ioLib.h"
#include "netinet/tcp.h"
LOCAL int echoServerPort = 7001; /* default port num - should be greater */
/*IMPORT int tcp_keepidle = 1;*/
STATUS echoTcpForever();
void echoTcpServerRun
(
int port /* port number; 0 to use the last used one */
)
{
if (port != 0)
echoServerPort = port;
FOREVER
if (echoTcpForever () == ERROR)
{
printf ("echoTcpServerRun: echoTcpForever failed\n");
return;
}
}
STATUS echoTcpForever ()
{
struct sockaddr_in serverAddr; /* server's address */
struct sockaddr_in newConnAddr; /* client's address */
int sock; /* socket fd */
int newConnection; /* socket fd */
int len; /* length of newConnAddr */
int numRead; /* number of bytes read */
int optionVal; /* value of setsocket option */
struct linger linger; /* amount of time to SO_LINGER */
char buffer [1024]; /* data buffer */
/*bzero ((char *) &serverAddr, sizeof (serverAddr));
bzero (buffer, sizeof (buffer));*/
/* open the TCP socket */
sock = bsdSocket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket");
return (ERROR);
}
optionVal = 1; /* Turn ON the diff. setsockopt options */
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &optionVal,
sizeof(optionVal)) == ERROR)
{
perror ("echoTcpForever: setsockopt SO_REUSEADDR failed");
close (sock);
return (ERROR);
}
if (setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, (char *) &optionVal,
sizeof(optionVal)) == ERROR)
{
perror ("echoTcpForever: setsockopt SO_KEEPALIVE failed");
close (sock);
return (ERROR);
}
if (setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *) &optionVal,
sizeof(optionVal)) == ERROR)
{
perror ("echoTcpForever: setsockopt TCP_NODELAY failed");
close (sock);
return (ERROR);
}
linger.l_onoff = 1; /* Turn ON the SO_LINGER option */
linger.l_linger = 0;/* Use default amount of time to shutdown i.e
the default value of TCP_LINGERTIME in tcp_timer.h*/
if (setsockopt (sock, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof (linger))
== ERROR)
{
perror ("echoTcpForever: setsockopt SO_LINGER failed");
close (sock);
return (ERROR);
}
/* Set up our internet address, and bind it so the client can connect. */
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons (echoServerPort);
serverAddr.sin_addr.s_addr = INADDR_ANY /*使用自己的地址*/;
/*bzero(&(serverAddr.sin_zero));*/ /*error checking for bind(): */
printf ("\nBinding SERVER :%x\n", serverAddr.sin_port);
if (bsdBind (sock, (struct sockaddr *) &serverAddr, sizeof (serverAddr)) < 0)
{
perror ("bind");
close (sock);
return (ERROR);
}
/* Listen for the client to connect to us. */
/* printf ("Listening to client\n");*/
if (bsdListen (sock, 5) < 0)
{
perror ("listen");
close (sock);
return (ERROR);
}
/* The client has connected. Accept, and receive chars */
len = sizeof (newConnAddr);
newConnection = bsdAccept (sock, (struct sockaddr *) &newConnAddr, &len);
if (newConnection == ERROR)
{
perror ("accept failed");
close (sock);
return (ERROR);
}
/* read data from the connection, and write the data back
* over the connection until the client terminates the transfer.
*/
while ((numRead = bsdRecv (newConnection, buffer, sizeof (buffer), 0)) > 0)
{
printf ("buffer is - %s and numRead = %d\n", buffer, numRead);
bsdSend (newConnection, buffer, numRead, 0);
}
/* When the peer end of the TCP socket is closed, recv system
* call will return 0 bytes (means EOF in TCP). When the connection is
* broken, recv will return ERROR with errno set to ETIMEDOUT.
*/
numRead = bsdRecv (newConnection, buffer, sizeof (buffer), 0);
if (numRead == 0)
{
printf ("No more bytes to read \n");
printf ("Closing the sockets\n");
}
else
{
printErrno (errnoGet ());
close (newConnection);
close (sock);
return (ERROR);
}
/* close the sockets */
close (newConnection);
close (sock);
return (OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -