Socket programming CHAT system

Discussion in 'C++' started by Sean, Jan 15, 2007.

  1. Sean

    Sean Guest

    Hi,
    I am trying to write a simple chat/text messaging program but I am
    having some problems. I am a rookie when it comes to socket programming
    so I am not sure if I am doing the write thing or not. Here is my code:


    #include <netdb.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <iostream>
    #include <pthread.h>

    #define MAX_LINE 256
    #define LINE_ARRAY_SIZE (MAX_LINE+1)
    #define SERVER_PORT 15001

    using namespace std;

    int sd;
    struct sockaddr_in serverSock;
    char buf[LINE_ARRAY_SIZE];

    int min(int arg1, int arg2);
    void setup();
    void * sendText(void* arg);
    void * recvText(void* arg);

    int main(){
    pthread_t sendTr, recvTr;
    struct sockaddr_in serverAddress;
    int socketDescriptor;
    char c;
    int len;

    setup();
    cout << "Enter the host's IP address: ";
    cin.get(buf, MAX_LINE, '\n');

    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(SERVER_PORT);
    inet_pton(AF_INET, buf, &serverAddress.sin_addr);
    socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0);

    pthread_create( &sendTr, NULL, sendText, NULL );
    //pthread_create( &recvTr, NULL, recvText, NULL );

    cout << "before while\n";
    while (strcmp(buf, ".")) {
    // Send the line to the server.

    cout << "in while0... \n";

    if( len > 0 && buf[len-1]=='\n'){
    buf[len-1] = 0; /* strip the newline from the end */
    len = min(strlen(buf), MAX_LINE); /* limit the size */

    sendto(socketDescriptor, buf, len, 0, (struct
    sockaddr*)&serverAddress, sizeof(serverAddress));
    cout << "in while1... \n";
    memset(buf, 0x0, LINE_ARRAY_SIZE);
    cout << "in while2... \n";
    // Read the modified line back from the server.
    if (recv(sd, buf, MAX_LINE, 0) < 0) {
    cerr << "didn't get response from server?";
    close(sd);
    exit(1);
    }
    }
    cout << "Recieved: " << buf << "\n";
    memset(buf, 0x0, LINE_ARRAY_SIZE);

    }
    return 0;
    }
    int min(int arg1, int arg2){
    if(arg1 > arg2)
    {
    return arg2;
    }
    else
    {
    return arg1;
    }
    }
    void setup(){

    sd = socket(AF_INET, SOCK_DGRAM, 0);

    memset(&serverSock, 0, sizeof(serverSock));
    serverSock.sin_family = AF_INET;
    serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
    serverSock.sin_port = htons(SERVER_PORT);

    if( bind(sd, (struct sockaddr *) &serverSock, sizeof(serverSock)) )
    puts( "bind() failed" );

    }
    void * sendText(void* arg){
    char c;
    cout << "thread started..\n";
    cout << "Input: ";
    cin.get(buf, MAX_LINE, '\n');
    while (cin.get(c) && c != '\n')
    ;
    cout << "thread finished...\n";
    }

    I guess I am not sure if I need to setup to sockets here. One for
    sending and One for receiving?
    Also I know that I need to use a threading system that way I can send
    and receive message simultaneously. But for some reason I get a
    segmentation fault.

    Any help about how I can improve the code, any issues, etc. would be
    much appreciated.

    Thanks

    J
     
    Sean, Jan 15, 2007
    #1
    1. Advertising

  2. Sean

    Guest

    Sean wrote:
    > Hi,
    > I am trying to write a simple chat/text messaging program but I am
    > having some problems. I am a rookie when it comes to socket programming
    > so I am not sure if I am doing the write thing or not. Here is my code:
    >
    > I guess I am not sure if I need to setup to sockets here. One for
    > sending and One for receiving?
    > Also I know that I need to use a threading system that way I can send
    > and receive message simultaneously. But for some reason I get a
    > segmentation fault.
    >

    You can try to have a look at some existing network library
    implementations, like mine - http://nnl.sf.net.
    As for threading - if processing of the messages don't take too much
    time and you only have 1 CPU machine I suggest you don't use threads..
    m
     
    , Jan 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,199
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,855
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    971
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    552
    Laszlo Nagy
    Feb 1, 2009
  5. Steve Holden
    Replies:
    0
    Views:
    666
    Steve Holden
    Feb 1, 2009
Loading...

Share This Page