473,626 Members | 3,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket programming CHAT system

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 socketDescripto r;
char c;
int len;

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

memset(&serverA ddress, 0, sizeof(serverAd dress));
serverAddress.s in_family = AF_INET;
serverAddress.s in_port = htons(SERVER_PO RT);
inet_pton(AF_IN ET, buf, &serverAddress. sin_addr);
socketDescripto r = 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(socketDe scriptor, buf, len, 0, (struct
sockaddr*)&serv erAddress, sizeof(serverAd dress));
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(&serverS ock, 0, sizeof(serverSo ck));
serverSock.sin_ family = AF_INET;
serverSock.sin_ addr.s_addr = htonl(INADDR_AN Y);
serverSock.sin_ port = htons(SERVER_PO RT);

if( bind(sd, (struct sockaddr *) &serverSock, sizeof(serverSo ck)) )
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
1 4990

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1397
by: Rob | last post by:
Hello, I've got a huge problem with async sockets. I've created a networked virtual environment, using async socket programming. The problem I face occurs when I close the listener socket. It listens using socket.BeginAccept(...). When a client disconnects, the socket is disconnected like this: try { socket.Shutdown(SocketShutDown.Receive);
3
392
by: Poiquiop | last post by:
Hi. I was wondering if there were any good tutorials related to socket programming, and handling different protocols, etc. Thanks, J.L.
2
6976
by: chellappa | last post by:
Hi Every body!, i did a small Chat program in Linux C Socket Programm ... I am using stdout/stdin i did in single machinee i will work properly..but i want to run in differnet machine..i tried ,but its faliure... try to modify this pgm run in differnt machine Server Program ============== #include <stdio.h> #include <sys/socket.h>
5
3679
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
9
8665
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should be faster than remoting and is a better choice for what I am doing but that it is difficult to code.
5
3989
by: bizt | last post by:
Hi, I have been asked by my manager to look into the possiblily of setting up an online chat system for the external website. We run an Apache server with PHP (hence the reason Im posting here) so it would need to run on that. Basically, it will run on the website and its purpose will allow members of the public to access the chat room and discuss current topics with experts. Similar to a doctors interactive website where
8
4671
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data using com component. It is just set of some characters like
5
7464
by: kokwoei82 | last post by:
can socket programming in vb.net use for data transfer such as login data? all the samples that i found on internet is just a chat program. can someone show me some sample code on simple login for user name and password to be transfered from client to server? thanks in advance.
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.