Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Sat May 04, 2024 9:57 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Need help with C codes
PostPosted: Mon Aug 13, 2007 11:33 am 
Offline
Kommunist
Kommunist

Joined: Tue Apr 24, 2007 2:16 pm
Posts: 10
Here are my C codes:
Code:
/***************************************************************************
 *   Copyright (C) 2007 by Cloud   *
 *   cloud@laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

#define MAX_BUFFER_SIZE 1024
#define MAX_CONNECTIONS 20

/**
* Function declarations
*/
void error (char *);
void makeServerSocket (char[] *, struct sockaddr_in &, struct sockaddr_in &, int &, int &, char &);
void makeClientSocket (char[] *, struct sockaddr_in &, struct sockaddr_in &, int &, int &, char &);

/**
 *
 */
int main(int argc, char *argv[])
{
    int socketType=0;
    int serverSocketFD=0;
    int clientSocketFD=0;
    int bytesRead=0;
    struct sockaddr_in client;
    struct sockaddr_in server;
    char buffer[MAX_BUFFER_SIZE];
   
    printf("Socket v0.01\n\n");
    /*if (sizeof(argv<1)) {
        printf("Usage: IP Address (xxx.xxx.xxx.xxx) Port Number (xxxxx)");
        exit(1);
    }*/
    printf("Select socket type (0=client, 1=Server)\n");
    scanf("%d", &socketType);
    if (socketType) {
        printf("Server type socket selected.\n\n");
        makeServerSocket(argv, server, client, serverSocketFD, clientSocketFD, buffer);
        while (1) {
            // make the server accecpt incoming messages
            clientSocketFD=accept(serverSocketFD, (struct sockaddr*) &client, sizeof(client));
            printf("%s:%d connected.\n", inet_ntoa(client.sin_addr), inet_ntohs(client.sin_port));
            // receive incoming message from the client
            recv(clientSocketFD, buffer, MAX_BUFFER_SIZE, 0);
            // check if client wanted to stop the connection
            if (*buffer=="quit")  {
                close(serverSocketFD);
                close(clientSocketFD);
            }
            // send back anything that was send
            send(clientSocketFD, buffer, MAX_BUFFER_SIZE, 0);
        }
    } else {
        printf("Client type socket selected.\n\n");
        makeClientSocket(argv, server, client, serverSocketFD, clientSocketFD, buffer);
        // send message to server
        printf("Enter message to send to server:\n");
        scanf("%s", &buffer);
        printf("Sending %s to %s:%d\n", buffer, inet_ntoa(server.sin_addr), inet_ntohs(server.sin_port));
        send(clientSocketFD, strcat(buffer, "\n"), sizeof(buffer), 0);
        // while there are data to be read
        do {
            bzero(buffer, MAX_BUFFER_SIZE);
            bytesRead=recv(clientSocketFD, buffer, MAX_BUFFER_SIZE, 0);
            printf("Incoming message from %s%d", inet_ntoa(server.sin_addr), inet_ntohs(server.sin_port), buffer);
        } while (bytesRead>0);
        // clean up
        close(clientSocketFD);
    }
    return EXIT_SUCCESS;
}

/**
*
*/
void error(char *message) {
    perror(message);
    exit(1);
}

/**
*
*/
void makeServerSocket (char[] *argv, struct sockaddr_in &server, struct sockaddr_in &client, int &serverSocketFD, int &clientSocketFD, char &buffer)
{
    // create server socket
    if ((serverSocketFD=socket(AF_INET, STREAM, 0))<1) error("Can not create server socket.\n");
    // set up socket connection
    bzero(&server, sizeof(&server));
    server.sin_family=AF_NET;
    server.sin_addr.s_addr=INADDR_ANY;
    server.sin_port=hton(argv[1]);
    // bind the socket to a port
    if (bind(serverSocketFD, (struct *sockaddr)&server, sizeof(server))==0) error("Can not bind socket to port.\n");
      // make the socket listen
    if (listen(serverSocketFD, MAX_CONNECTIONS)==0) error("Can not make socket listen to incoming messages.\n");
}

/**
 *
 */
void makeClientSocket(char[] *argv, struct sockaddr_in &server, struct sockaddr_in &client, int &serverSocketFD, int &clientSocketFD, char &buffer)
{
    // create client socket
    if ((clientSocketFD=socket(AF_INET, STREAM, 0) < 1)) error("Can not create client socket");
    // set up server connection
    server.sin_family=AF_NET;
    server.sin_port=htons(argv[1]);
    if (inet_aton(argv[0], &server.sin_addr.s_addr)==0) error("Server IP address error.\n");
    // connect the client to the server
    if (connect(clientSocketFD, (struct sockaddr*)&server, sizeof(server))!=0) error("Can not connect to the server.\n");
}



These are the errors that I got when I try to compile my C program with GCC:
Code:
sockets.c:39: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
sockets.c:40: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
sockets.c: In function ‘main’:
sockets.c:64: warning: passing argument 3 of ‘accept’ makes pointer from integer without a cast
sockets.c:69: warning: comparison between pointer and integer
sockets.c: At top level:
sockets.c:107: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
sockets.c:125: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token


Can anyone help me fix the errors in my C codes for me?


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 13, 2007 2:51 pm 
I'm having trouble believing you actually wrote this. The majority of your errors is caused by the same mistake repeated, and is something rather basic. char[] * in the function prototypes should by char *[]


Top
  
Reply with quote  
PostPosted: Tue Aug 14, 2007 12:33 pm 
Offline
Kommunist
Kommunist

Joined: Tue Apr 24, 2007 2:16 pm
Posts: 10
dlong wrote:
I'm having trouble believing you actually wrote this. The majority of your errors is caused by the same mistake repeated, and is something rather basic. char[] * in the function prototypes should by char *[]


OK thanks Dlong that fixed some of the errors, but not all the errors.

It seems that I can't declare the function prototypes like this:
Code:
void makeServerSocket (char[] *, struct sockaddr_in &, struct sockaddr_in &, int &, int &, char &);
void makeClientSocket (char[] *, struct sockaddr_in &, struct sockaddr_in &, int &, int &, char &);


Instead I have to declare it like this and it worked
Code:
void makeServerSocket (char *[], struct sockaddr_in *, struct sockaddr_in *, int, int, char);
void makeClientSocket (char *[], struct sockaddr_in *, struct sockaddr_in *, int, int, char);


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group