server.c 813 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include<stdio.h>
  2. #include<sys/socket.h>
  3. #include<sys/types.h>
  4. #include<netinet/in.h>
  5. #include<unistd.h>
  6. #include<string.h>
  7. int main()
  8. {
  9. int ssd,sd,bi;
  10. int port;
  11. struct sockaddr_in server;
  12. char msg[80];
  13. sd=socket(AF_INET,SOCK_STREAM,0);
  14. bzero(&server,sizeof(server));
  15. server.sin_family=AF_INET;
  16. printf("enter the port no");
  17. scanf("%d",&port);
  18. server.sin_port=htons(port);
  19. server.sin_addr.s_addr=INADDR_ANY;
  20. bi=bind(sd,(struct sockaddr*)&server,sizeof(server));
  21. listen(sd,1);
  22. int i=sizeof(server);
  23. ssd=accept(sd,(struct sockaddr*)&server,&i);
  24. while(1)
  25. {
  26. printf("\nenter the message to send\n");
  27. scanf("%s",msg);
  28. write(ssd,msg,strlen(msg));
  29. printf("\nmsg from client\n");
  30. memset(msg,'\0',sizeof(msg));
  31. read(ssd,msg,10);
  32. printf("%s",msg);
  33. }
  34. close(sd);
  35. close(ssd);
  36. return 0;
  37. }