Question:

write a socket program for echo/ping/talk commands

by Guest2875  |  8 years, 7 month(s) ago

1 LIKES UnLike

programing language

 Tags: Commands, echopingtalk, Program, socket, write

   Report

1 ANSWERS

  1. Guest2038
    EchoClient.c
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<sys/un.h>
    #include<arpa/inet.h>
    #include<netinet/in.h>
    #define SOCK_PATH "echo_socket"
    int main(void)
    {
    int s1,t,len;
    struct sockaddr_un remote;
    char str[100];
    if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
    {
    printf("socket\n");
    exit(1);
    }
    printf("Trying to connect\n");
    remote.sun_family=AF_UNIX;
    strcpy(remote.sun_path,SOCK_PATH);
    len=strlen(remote.sun_path)+sizeof(remote.sun_family);
    if(connect(s1,(struct sockaddr*)&remote,len)==-1)
    {
    perror("connect");
    exit(1);
    }
    printf("Connectd\n");
    while(printf("->"),fgets(str,100,stdin),!feof(stdin))
    {
    if(send(s1,str,strlen(str),0)==-1)
    {
    perror("send");
    exit(1);
    }
    if((t=recv(s1,str,100,0))>0)
    {
    str[t]='\0';
    printf(" echo->%s",str);
    }
    else
    {
    if(t<0)
    perror("recv");
    else
    printf("server closed connection\n");
    exit(1);
    }
    }
    close(s1);
    return 0;
    }




    //EchoServer.c
    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<sys/un.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #define SOCK_PATH "echo_socket"
    int main(void)
    {
    int s1,s2,len;
    struct sockaddr_un local;
    struct sockaddr_un remote;
    char str[100];
    if((s1=socket(AF_UNIX,SOCK_STREAM,0))==-1)
    {
    printf("socket\n");
    exit(1);
    }
    local.sun_family=AF_UNIX;
    strcpy(local.sun_path,SOCK_PATH);
    unlink(local.sun_path);
    len=strlen(local.sun_path)+sizeof(local.sun_family);
    if(bind(s1,(struct sockaddr*)&local,len)==-1)
    {
    printf("Bind");
    exit(1);
    }
    if(listen(s1,5)==-1)
    {
    printf("listen");
    exit(1);
    }
    for(;;)
    {
    int done,n,t;
    printf("Waiting for connection\n");
    t=sizeof(remote);
    if((s2=accept(s1,(struct sockaddr*)&remote,&t))==-1)
    {
    perror("accept");
    exit(1);
    }
    printf("Connected\n");
    done=0;
    do
    {
    n=recv(s2,str,100,0);
    if(n<=0)
    {
    if(n<0)
    perror("recv");
    done=1;
    }
    if(!done)
    if(send(s2,str,n,0)<0)
    {
    perror("send");
    done=1;
    }
    }
    while(!done);
    close(s2);
    }
    return 0;
    }











    //output

    [exam01@localhost ~]$ cc EchoServer.c
    EchoServer.c: In function ΓÇÿmainΓÇÖ:
    EchoServer.c:41: warning: pointer targets in passing argument 3 of ΓÇÿacceptΓÇ
    differ in signedness
    [exam01@localhost ~]$ ./a.out 172.16.0.143
    Waiting for connection
    Connected

    //output
    [exam01@localhost ~]$ cc EchoClient.c
    [exam01@localhost ~]$ cc EchoClient.c
    [exam01@localhost ~]$ ./a.out 172.16.0.143
    Trying to connect
    Connectd
    ->hiii
    echo->hiii
    ->what u want?
    echo->what u want?
    ->india is my country
    echo->india is my country
    ->

Sign In or Sign Up now to answser this question!

Question Stats

Latest activity: 9 years, 5 month(s) ago.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.