Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Socket programming CHAT system

Reply
Thread Tools

Socket programming CHAT system

 
 
Sean
Guest
Posts: n/a
 
      01-15-2007
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

 
Reply With Quote
 
 
 
 
Mehturt@gmail.com
Guest
Posts: n/a
 
      01-15-2007

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
Chat Server and Chat Client for INTRANET diptanu@gmail.com Java 0 01-30-2006 06:56 PM



Advertisments