fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<netinet/in.h>
  4. #include<sys/types.h>
  5. #include<sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include<unistd.h>
  8. #include<string.h>
  9.  
  10. int main(){
  11. int sockfd;
  12. struct sockaddr_in serv_addr;
  13. char a[50];
  14. sockfd= socket(AF_INET, SOCK_STREAM, 0);
  15. if(sockfd<0){
  16. printf("socket failed");
  17. exit(0);
  18. }
  19. serv_addr.sin_family=AF_INET;
  20. serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
  21. serv_addr.sin_port=htons(3100);
  22. if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0){
  23. printf("connection failed");
  24. exit(0);
  25. }
  26. printf("Enter the message:\n");
  27. fgets(a,50,stdin);
  28. write(sockfd,a,50);
  29. read(sockfd,a,50);
  30. printf("Server Received : %s",a);
  31. close(sockfd);
  32. }
  33.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
connection failed