#include #include #include #include #include #include #include #define MAXLINE 4096 /*max text line length*/ #define SERV_PORT 3000 /*port*/ int main(int argc, char **argv) { int sockfd, len, n; struct sockaddr_in servaddr; char sendline[MAXLINE], recvline[MAXLINE]; //basic check of the arguments //additional checks can be inserted if (argc !=2) { perror("Usage: TCPClient "); exit(1); } //Create a socket for the client //If sockfd<0 there was an error in the creation of the socket if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) <0) { perror("Problem in creating the socket"); exit(2); } //Creation of the socket memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= inet_addr(argv[1]); servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order while (fgets(sendline, MAXLINE, stdin) != NULL) { len = sizeof(servaddr); sendto(sockfd, sendline, strlen(sendline), 0, (struct sockaddr *)&servaddr, len); n = recvfrom(sockfd, recvline, MAXLINE,0, (struct sockaddr *)&servaddr, &len); recvline[n] = '\0'; printf("%s", "String received from the server: "); fputs(recvline, stdout); } exit(0); }