Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/socket.h> /* for socket(), bind(), and connect() */
- #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <iostream>
- using namespace std;
- #define MAXPENDING 5
- void DieWithError(char *errorMessage);
- void HandleTCPClient(int clntSocket);
- /* Maximum outstanding connection requests */
- int main() {
- printf("fadyyyy");
- /* TCP client handling function */
- int servSock; /* Socket descriptor for server */
- int clntSock; /* Socket descriptor for client */
- struct sockaddr_in echoServAddr; /* Local address */
- struct sockaddr_in echoClntAddr; /* Client address */
- unsigned short echoServPort; /* Server port */
- unsigned int clntLen; /* Length of client address data structure */
- echoServPort = 5000; /* First arg: local port */
- /* Create socket for incoming connections */
- if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- DieWithError("socket () failed");
- /* Construct local address structure */
- memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
- echoServAddr.sin_family = AF_INET; /* Internet address family */
- echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
- echoServAddr.sin_port = htons(echoServPort); /* Local port */
- cout << echoServAddr.sin_addr.s_addr << endl;
- /* Bind to the local address */
- if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0){
- DieWithError("bind () failed");
- }
- /* Mark the socket so it will listen for incoming connections */
- if (listen(servSock, MAXPENDING) < 0)
- DieWithError("listen() failed");
- while (1) /* Run forever */
- {
- cout << "TOMIA" << endl;
- /* Set the size of the in-out parameter */
- clntLen = sizeof(echoClntAddr);
- cout << "TOMIA++" << endl;
- /* Wait for a client to connect */
- // if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
- // DieWithError("accept() failed");
- if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
- DieWithError("accept() failed");
- cout << "TIA" << endl;
- /* clntSock is connected to a client! */
- printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
- HandleTCPClient(clntSock);
- }
- /* NOT REACHED */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement