little help in simple socket library

This is a discussion on little help in simple socket library within the Networking/Device Communication forums, part of the General Programming Boards category; I made simple socket library for use in future, it can be compile but when I create a simple server ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    little help in simple socket library

    I made simple socket library for use in future, it can be compile but when I create a simple server based on it, it fail to work.
    Code:
    #ifndef _OPENSOCKET_H_
    #define _OPENSOCKET_H_
    
    /* handle for socket */
    typedef int hSOCK;
    
    /* socket creation */
    typedef struct __sock_s
     {
    	 int domain;
    	 int type;
    	 int protocol;
     } *sock_s;
    
    /* struct to hold socket information */
    typedef struct sockaddr_in *sock_i;
    
    
    hSOCK socket_open(sock_s ssock);
    void socket_init(sock_i tsock);
    int socket_listen(hSOCK sock, sock_i tsock, int family, const char *address, unsigned int port);
    hSOCK socket_accept(hSOCK sock, sock_i csock);
    int socket_connect(hSOCK sock, sock_i tsock);
    int socket_send_len(hSOCK csock, char *len, int flags);
    int socket_send(hSOCK sock, char *data, unsigned int len, int flags);
    int socket_recv_len(hSOCK sock, int flags);
    char *socket_recv(hSOCK sock, int len, int flags);
    int socket_close(hSOCK sock);
    
    #endif
    OpenSocket.c:
    Code:
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    typedef int hSOCK;
    
    /* socket creation */
    typedef struct __sock_s
     {
    	 int domain;
    	 int type;
    	 int protocol;
     } *sock_s;
    
    /* struct to hold socket information */
    typedef struct sockaddr_in *sock_i;
    
    /* Create socket and return handle */
    hSOCK socket_open(sock_s ssock)
     {
    	 int fd;
    	 fd = socket(ssock->domain, ssock->protocol, ssock->type);
    	 if(!fd)
    	  return -1;
    	 else
    	  return (fd);
     }
    
    /* Initializing socket */
    void socket_init(sock_i tsock)
     {
    	 memset(&tsock, 0, sizeof(tsock));
     }
    
    /* Setting socket information and listening in given port */
    int socket_listen(hSOCK sock, sock_i tsock, int family, const char *address, unsigned int port)
     {
    	int stat;
    	tsock->sin_family = family;
    	tsock->sin_addr.s_addr = inet_addr(address);
    	tsock->sin_port = htons(port);
    	stat = bind(sock, (struct sockaddr*)&tsock, sizeof(tsock));
    	if(stat == -1)
    	 return (stat);
    	stat = listen(sock, 5);
    	if(stat == -1)
    	 return -2;
    	else
    	 return (stat);
     }
    
    /* Accept a connection from client and return handle to that */
    hSOCK socket_accept(hSOCK sock, sock_i csock)
     {
    	int hCLT;
    	socklen_t len;
    	len = sizeof(csock);
    	hCLT = accept(sock, (struct sockaddr*)&csock, &len);
    	if(!hCLT)
    	 return -1;
        else
    	 return (hCLT);
     }
    
    /* Connect to a host */
    int socket_connect(hSOCK sock, sock_i tsock)
     {
         int stat;
         stat = connect(sock, (struct sockaddr*)&tsock, sizeof(tsock));
    	 if(!stat)
    	  return -1;
         else
    	 return (stat);
     }
     
    /* Send the next size of data to server */
    int socket_send_len(hSOCK sock, char *len, int flags)
     {
         int stat;
         stat = send(sock, len, sizeof(len), flags);
    	 if(!stat)
    	  return -1;
         else
    	 return (stat);
     }
    
    /* Send the actual data */
    int socket_send(hSOCK sock, char *data, unsigned int len, int flags)
     {
         int stat;
         stat = send(sock, data, len, flags);
    	 if(!stat)
    	  return -1;
         else
    	  return (stat);
     }
    
    /* Recieve the amount of data to recieve from the peer */
    int socket_recv_len(hSOCK sock, int flags)
     {
         int stat;
         unsigned int len;
         char *Buffer = NULL;
         Buffer = (char*)malloc(sizeof(int));
         if(!Buffer)
          return -1;
         stat = recv(sock, Buffer, sizeof(int), flags);
    	 if(!stat)
    	  return -2;
         else
          {
              len = atoi(Buffer);
              return (len);
          }
     }
    
    /* Receive data from the peer and return pointer to it */
    char *socket_recv(hSOCK sock, int len, int flags)
     {
         int stat;
         char *Buffer = NULL;
         Buffer = (char*)malloc(sizeof(char)*len);
         if(!Buffer)
          return NULL;
         stat = recv(sock, Buffer, len, flags);
    	 if(!stat)
    	  return NULL;
         else
          return (Buffer);
     }
     
    int socket_close(hSOCK sock)
     {
    	 return close(sock);
     }
    Makefile:
    Code:
    CC= cc
    CFLAGS= -c -Wall
    AR= ar
    libSPLT.o: OpenSocket.c OpenSocket.h
    	$(CC) OpenSocket.c $(CFLAGS)
    
    install:
    	$(AR) rc libOpenSocket.a OpenSocket.o
    	cp libOpenSocket.a /usr/local/lib
    	cp OpenSocket.h /usr/local/include/OpenSocket.h
    uninstall:
    	rm /usr/local/lib/OpenSocket.a
    	rm /usr/local/include/OpenSocket.h
    .PHONY: clean
    clean:
    	rm -f *.o
    	rm -f *.a
    stest.c:
    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<OpenSocket.h>
    
    main()
    {
    sock_i Server;
    sock_s socket;
    hSOCK sock, stat;
    socket->domain = AF_INET;
    socket->type = SOCK_STREAM;
    socket->protocol = 0;
    socket_init(Server);
    sock = socket_open(socket);
    if(sock<0)
     printf("error to create socket\n");
    stat = socket_listen(sock, Server, AF_INET, "127.0.0.1", 5000);
    if(stat<0)
     printf("error in listening\n");
    socket_close(sock);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,767
    Where do you allocate space for the structure sock_s/socket points to?

    Where do you include the header file you created?
    Why do you rewrite the header code in your source code file?

    Note: Declaring sock_s as a pointer is something I consider bad style.

    Note: read "Casting malloc" http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Tim S.
    Last edited by stahta01; 12-31-2010 at 11:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use one socket to send and receive message (using UDP)
    By tanya9x in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2010, 05:58 AM
  2. Simple Socket Question
    By SourceCode in forum Linux Programming
    Replies: 2
    Last Post: 06-22-2003, 10:20 PM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 12:48 PM
  4. simple socket problem.
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 01-14-2003, 06:20 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21