Hax4Us

Untitled

Sep 8th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #define MAX 1024
  8. int main(){
  9. struct sockaddr_in sa;
  10. int sockfd, n_sfd,readval;
  11. char serverMsg[MAX]={'0'},clientMsg[MAX];
  12. socklen_t salen=sizeof(sa);
  13. sockfd=socket(AF_INET,SOCK_STREAM,0);
  14. sa.sin_family=AF_INET;
  15. sa.sin_port=htons(4444);
  16. inet_pton(AF_INET,"0.0.0.0",&(sa.sin_addr));
  17.  
  18. if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0){
  19. puts("Bind");
  20. return -1;}
  21.  
  22. listen(sockfd,3);
  23. n_sfd=accept(sockfd,(struct sockaddr*)&sa,&salen);
  24. int pid=fork();
  25.  
  26. if(pid==0){
  27. while(1){
  28. if(strlen(clientMsg)>1){
  29. printf("\nClient : %s",clientMsg);
  30. strcpy(clientMsg,"0");
  31. }
  32. }
  33. }
  34. else{
  35. while(1){
  36. printf("\ntype something ... ");
  37. fgets(serverMsg,MAX,stdin);
  38. send(n_sfd,serverMsg,strlen(serverMsg),0);
  39. }
  40. }
  41. }
Add Comment
Please, Sign In to add comment