Socket programming CHAT system

S

Sean

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
 
M

Mehturt

Sean said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top