Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <netdb.h>
- #define MAX 1024
- #define PORT 19955
- int main(void){
- int sockfd, status;
- char serverMsg[MAX]={0},clientMsg[MAX]={0};
- struct addrinfo hints,*res,*p;
- hints.ai_family=AF_UNSPEC;
- hints.ai_socktype=SOCK_STREAM;
- if((status=getaddrinfo("0.tcp.ngrok.io","19955",&hints,&res))!=0){
- fprintf(stderr,"getaddrinfo : %s",gai_strerror(status)); return -1;}
- for(p=res;p!=NULL;p=p->ai_next){
- if((sockfd=socket(p->ai_family,p->ai_socktype,p->ai_protocol))<0){
- printf("socket() failed");
- continue;}
- if(connect(sockfd,p->ai_addr,p->ai_addrlen)<0){
- printf("Con() Failed");
- close(sockfd);
- sockfd=-1;
- continue;}
- break; // we have correct IP now break the loop
- }
- if(sockfd<0){
- printf("try again !");
- return -1;}
- int pid=fork();
- if(pid==0){
- while(1){
- read(sockfd,serverMsg,MAX);
- if(strlen(serverMsg)>1){
- printf("Server : %s\n",serverMsg);
- puts("type something ...");
- }
- }
- }
- else{
- while(1){
- printf("type msg here ... ");
- fgets(clientMsg,MAX,stdin);
- send(sockfd,clientMsg,strlen(clientMsg)+1,0);
- }
- }
- }
Add Comment
Please, Sign In to add comment