Current location: Hot Scripts Forums » Programming Languages » C/C++ » Recieving and Sending


Recieving and Sending

Reply
  #1 (permalink)  
Old 08-16-07, 02:05 PM
Shokora Shokora is offline
New Member
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Recieving and Sending

I have a problem with a console based chat application i'm writing to practice with sockets. I have a client/server model now, but only the client can send and only the server can recieve messages. I need to make 1 application able to send and recieve at the same time.

I tried to do it with select(); but i got stuck so that didn't really work out well.

Here is the code for both the applications untill now:

Code:
/*******************************************************************************
 *Author: Shokora
 *Date: 16/08/2007
 *Filename: server.c 
 *Description: A server for a basic client/server model  
 *******************************************************************************/ 

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

#define MY_PORT 3334
#define BACKLOG 10
#define EXIT "exit"

int main()
{
    //Declare the file descriptors
    int sockfd, new_fd;
    //Make the local structure
    struct sockaddr_in my_addr;
    //Make the remote structure
    struct sockaddr_in their_addr;
    //Declare all the other ints
    int sin_size, control = 0, rn;
    //The buffer for the message sent to us by the client
    char buffer[500], message[500];
    
    //make the socket
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("Could not make socket\n");
        return -1;
    }
    else
    {
        printf("Socket successfully made\n");
    }
    
    //Set all the things in the local structure as they should
    my_addr.sin_family = AF_INET;
    //The port needs to be in network byte order
    my_addr.sin_port = htons(MY_PORT);
    //Accept any connection
    my_addr.sin_addr.s_addr = INADDR_ANY;
    //Make the sin.zero all zeroes
    memset(&(my_addr.sin_zero),0,8);
    
    //Bind the socket
    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
    {
        perror("Could not bind socket\n");
        return -1;
    }
    else
    {
        printf("Socket successfully bound \n");
    }
       
    //Listen on the socket
    if(listen(sockfd, BACKLOG) == -1)
    {
        perror("Could not listen on socket\n");
        return -1;
    }
    else
    {
        printf("Listening...\n");
    }
    
    //Accept connections
    sin_size = sizeof(struct sockaddr_in);
    if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
    {
        perror("Could not accept connection\n");
    }
    else
    {
        printf("Connection accepted\n");
        
        //If we have a connection, start recieving messages
        while(control == 0)
        {
            bzero(buffer,500);
            
                if(rn = recv(new_fd, buffer, 500, 0) == -1)
                {
                    perror("There was an error reading data\n");
                    return -1;
                }
                else
                {
                    printf("%s\n",buffer);
                    if(strcmp(buffer,EXIT) == 0)
                    {
                        control = 1;
                    }
                 }
        }
        
        close(sockfd);
        close(new_fd);
    }
    
    return 0;
}
Code:
/*******************************************************************************
 *Author: Shokora
 *Date: 16/08/2007
 *Filename: client.c 
 *Description: A client for a basic client/server model  
 *******************************************************************************/ 

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

#define THEIR_PORT 3334
#define THEIR_ADDR "127.0.0.1"
#define EXIT "exit"

//Function prototypes
char StripNewline(char *string);

int main()
{
    //Declare file descriptors
    int sockfd;
    //Make the main structure
    struct sockaddr_in my_addr;
    //The buffer for the message
    char buffer[500];
    int len, send_len, control = 0;
    
    //make the socket
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("Could not make socket\n");
        return -1;
    }
    else
    {
        printf("Socket successfully made\n");
    }
    
    //Set all the things in the local structure as they should
    my_addr.sin_family = AF_INET;
    //The port needs to be in network byte order
    my_addr.sin_port = htons(THEIR_PORT);
    //Needs to be in network format
    my_addr.sin_addr.s_addr = inet_addr(THEIR_ADDR);
    //Make the sin.zero all zeroes
    memset(&(my_addr.sin_zero),0,8);
    
    //Connect the socket, it'll bind on a random port
    if(connect(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
    {
        perror("Could not connect to the server\n");
        return -1;
    }
    else
    {
        printf("Successfully connected to the server\n");
        
        while(control == 0)
        {
            //If the connection is succesfully made write something to the server
            printf("What do you want to say to the server?\n");
            StripNewline(fgets(buffer,500,stdin));
            
            len = strlen(buffer);
            if((send_len = send(sockfd, buffer, len, 0)) == -1)
            {
                perror("Could not send the message\n");
                return -1;
            }
            else if(len != send_len)
            {
                printf("We did send something but not all of it \n");
                printf("Message: %d bytes \n Sent: %d bytes\n",len,send_len);
            }
            
            if(strcmp(buffer,EXIT) == 0)
            {
                control = 1;
            }
        }
        
    }
    
    return 0;
}

char StripNewline(char *string)
{
    if(string[strlen(string) - 1] == '\n')
    {
        string[strlen(string) - 1] = '\0';
    }
    
    return string;
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 04:39 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.