diff --git a/sock-options/getsockopt.c b/sock-options/getsockopt.c new file mode 100644 index 0000000..de563a3 --- /dev/null +++ b/sock-options/getsockopt.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +main() +{ + int optlen, gs, socktype, s; + + /* Create a datagram socket. */ + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + perror("Socket not created"); + return EXIT_FAILURE; + } + + /* Ask for the socket type. */ + optlen = sizeof(socktype); + gs = getsockopt (s, SOL_SOCKET, SO_TYPE, &socktype, &optlen); + if (gs == -1) { + perror("getsockopt failed"); + return EXIT_FAILURE; + } + /* Print socket type. */ + switch (socktype) + { + case SOCK_STREAM: + puts("Stream socket.\n"); + break; + case SOCK_DGRAM: + puts("Datagram socket.\n"); + break; + case SOCK_RAW: + puts("Raw socket.\n"); + break; + default: + puts("Unknown socket type.\n"); + break; + } + return EXIT_SUCCESS; +} diff --git a/tcp-echo-improved/echo-client.c b/tcp-echo-improved/echo-client.c deleted file mode 100644 index d2cbb0e..0000000 --- a/tcp-echo-improved/echo-client.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include -#include - -#define MAXLINE 1001 -#define SERV_IP "127.0.0.1" -#define SERV_PORT 9002 - -void str_echo(int sockfd) -{ - // ssize_t n; - // char line[MAXLINE]; - // for(;;) - // { - // scanf(" %s", line); - // send(sockfd, line , n, 0); - // recv(sockfd, line , MAXLINE,0); - // printf("\n\n from server -- %s", line); - // } - - char sendline[100]; - char recvline[100]; - while(fgets(sendline,100,stdin)!=NULL) - { - - //fgets(sendline,100,stdin); /*stdin = 0 , for standard input */ - - //send(sockfd,sendline,strlen(sendline)+1,0); - write(sockfd,sendline,strlen(sendline)+1);/*send and write behave in the same way,just the flag argument is set to 0*/ - read(sockfd,recvline,100); - printf("%s",recvline); - bzero( sendline, 100); - bzero( recvline, 100); - } - - -} - -int main(){ - int sockfd; - - struct sockaddr_in server; - sockfd=socket(AF_INET,SOCK_STREAM,0); - bzero(&server,sizeof(server)); - server.sin_family=AF_INET; - server.sin_addr.s_addr=inet_addr(SERV_IP); - server.sin_port=htons(SERV_PORT); - - connect(sockfd,(struct sockaddr*)&server,sizeof(server)); - str_echo(sockfd); - -} diff --git a/tcp-echo-improved/echo-server-improved.c b/tcp-echo/echo-server-improved.c similarity index 100% rename from tcp-echo-improved/echo-server-improved.c rename to tcp-echo/echo-server-improved.c