By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Socket programming CHAT system

P: n/a
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

Jan 15 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.