Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <string.h>
- #include <unistd.h>
- #define bufferSize 1024
- int main(int argc, char *argv[])
- {
- if(argc<2)
- {
- printf("Error. No port was entered.\n");
- fflush(stdout);
- exit(1);
- }
- int port = atoi(argv[1]);
- char buffer[bufferSize];
- /*int server_fd = socket(AF_INET,SOCK_DGRAM,0);
- struct sockaddr_in server,client;
- if(server_fd<0)
- {
- if(server_fd)
- {
- close(server_fd);
- }
- printf("Error. Couldn't create socket on server.\n");
- exit(1);
- }
- server.sin_family = AF_INET;
- server.sin_port = htons(port);
- server.sin_addr.s_addr=htonl(INADDRY_ANY);
- if(bind(server_fd,(struct sockaddr *) &server, sizeof(server))<0)
- {
- printf("Error. Binding datagram socket server_fd to port %d failed.\n",port);
- if(server_fd)
- {
- close(server_fd);
- }
- exit(1);
- }
- while(1)
- {
- socklen_t client_len = sizeof(client);
- while(1)
- {
- int readBytes = recvfrom(server_fd,buffer,bufferSize,0,(struct sockaddr *)&client,&client_len);
- if(readBytes<=0)
- {
- printf("Error. Could not read data from client.\n");
- break;
- }
- buffer[readBytes]='\0';
- int sentBytes = sendto(server_fd,buffer,strlen(buffer)+1,0,(struct sockaddr *)&client, &client_len);
- if(sentBytes<=0)
- {
- printf("Error. Could not send data to client.\n");
- break;
- }
- }
- }*/
- int server_fd, client_fd, checker;
- struct sockaddr_in server, client;
- server_fd = socket(AF_INET,SOCK_STREAM,0);
- if(server_fd < 0)
- {
- printf("Couldn't create socket.\n");
- fflush(stdout);
- exit(EXIT_FAILURE);
- }
- server.sin_family=AF_INET;
- server.sin_port=htons(port);
- server.sin_addr.s_addr = htonl(INADDR_ANY);
- int reuseOn=1;
- if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&reuseOn,sizeof(reuseOn)) < 0)
- {
- printf("Error. setsockopt(SO_REUSEADDR) failed.\n");
- fflush(stdout);
- exit(EXIT_FAILURE);
- }
- if(bind(server_fd,(struct sockaddr *) &server, sizeof(server))<0)
- {
- printf("Error. Binding server_fd to port %d failed.\n",port);
- fflush(stdout);
- exit(EXIT_FAILURE);
- }
- checker = listen(server_fd,128);
- if(checker<0)
- {
- printf("Error. Listening on socket failed.\n");
- fflush(stdout);
- }
- printf("Echo server is listening on port %d.\n",port);
- fflush(stdout);
- int dataSendCounter=0;
- int dataReceivedCounter=0;
- while(1)
- {
- socklen_t client_len = sizeof(client);
- client_fd = accept(server_fd,(struct sockaddr*) &client,&client_len);
- if(client_fd<0)
- {
- printf("Error. Failed to establish new connection.");
- fflush(stdout);
- continue;
- }
- while(1)
- {
- printf("\nReceiving new data.\n");
- int readBytes = recv(client_fd,buffer,bufferSize,0);
- dataReceivedCounter++;
- printf("\n Times received data = %d \n",dataReceivedCounter);
- if(readBytes<=0)
- {
- printf("Error. Connection with client lost. Could not read message. readBytes = %d \n",readBytes);
- fflush(stdout);
- break;
- }
- printf("\nREADBYTES = %d\n",readBytes);
- buffer[readBytes]='\0';
- printf("From client: %s ",buffer);
- fflush(stdout);
- int timesSent=0;
- int bytesSent=0;
- int totalBytesSent=0;
- while(1)
- {
- bytesSent=totalBytesSent;
- if(totalBytesSent<readBytes)
- {
- timesSent++;
- printf("\nSending data.\n");
- totalBytesSent+=send(client_fd,buffer+bytesSent,readBytes-bytesSent,0);
- dataSendCounter++;
- printf("\nTimes sent data = %d\n",dataSendCounter);
- if(totalBytesSent<=bytesSent)
- {
- printf("Error. Could not send message.\n");
- fflush(stdout);
- close(client_fd);
- break;
- }
- printf("bytesSent = %d\n",bytesSent);
- printf("debug %d\n",timesSent);
- //close(client_fd);
- }
- else
- {
- break;
- }
- }
- printf("To client: %s \n", buffer);
- fflush(stdout);
- if(strncmp(buffer,"over",4)==0)
- {
- printf("Exchange with client ended successfully.\n");
- fflush(stdout);
- break;
- }
- }
- close(client_fd);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement