Server side implementation of Server-Client system.
More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
|
#define | MAX 80 |
| max. More...
|
|
#define | PORT 8080 |
| port number to connect to
|
|
#define | SA struct sockaddr |
| shortname for sockaddr
|
|
|
void | func (int sockfd) |
| Continuous loop to send and receive over the socket. More...
|
|
int | main () |
| Driver code. More...
|
|
Server side implementation of Server-Client system.
- Author
- Nairit11
-
Krishna Vedala
- See also
- client_server/client.c
◆ MAX
max.
characters per message
◆ func()
Continuous loop to send and receive over the socket.
Exits when "exit" is sent from commandline.
- Parameters
-
sockfd | socket handle number |
57 read(sockfd, buff,
sizeof(buff));
59 printf(
"From client: %s\t To client : ", buff);
63 while ((buff[n++] = getchar()) !=
'\n')
69 write(sockfd, buff,
sizeof(buff));
72 if (strncmp(
"exit", buff, 4) == 0)
74 printf(
"Server Exit...\n");
#define MAX
max.
Definition: server.c:33
◆ main()
Driver code.
86 if (WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
88 perror(
"WSA Startup error: \n");
97 struct sockaddr_in servaddr, cli;
100 sockfd = socket(AF_INET, SOCK_STREAM, 0);
103 perror(
"socket creation failed...\n");
108 printf(
"Socket successfully created..\n");
110 bzero(&servaddr,
sizeof(servaddr));
113 servaddr.sin_family = AF_INET;
114 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
115 servaddr.sin_port = htons(
PORT);
118 if ((bind(sockfd, (
SA *)&servaddr,
sizeof(servaddr))) != 0)
120 perror(
"socket bind failed...\n");
125 printf(
"Socket successfully binded..\n");
129 if ((listen(sockfd, 5)) != 0)
131 perror(
"Listen failed...\n");
136 printf(
"Server listening..\n");
141 connfd = accept(sockfd, (
SA *)&cli, &len);
144 perror(
"server acccept failed...\n");
149 printf(
"server acccept the client...\n");
#define SA
shortname for sockaddr
Definition: server.c:35
#define PORT
port number to connect to
Definition: server.c:34
void func(int sockfd)
Continuous loop to send and receive over the socket.
Definition: server.c:47