i'was asked to develop chat application using c++
anyone can help me finding tutorials/ articles/ walkthrough or what ever i can get that discuss this topic
Clockowl 56 Posting Whiz
First post!
Read the sticky at the top of the forum?
Freaky_Chris 299 Master Poster
I agree with clockowl.
Anyway, when you say you were asked....by who your lecturer. Something tells me you wouldn't be asked to do something like this if you hadn't already been taught most if not all the material you need to do this. I suggest you look at winsock. It might be a good place for you to start...presuming your developing on windows and for windows OS's of course we don't know these things.
Chris
Clockowl 56 Posting Whiz
Here are two examples I created and use as a reference, maybe they are of help:
/*
** Playing around with WinSockets
*/
#include <stdio.h>
#include <winsock.h>
int main (int argc, char **argv){
WORD wsaVersion;
wsaVersion = MAKEWORD(2,2);
WSADATA wsaData;
SOCKET sock1;
int wsaerr;
wsaerr = WSAStartup(wsaVersion, &wsaData);
if (wsaerr != 0){
printf("\nWinsock not found");
return -1;
}
printf("Winsock found");
sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock1 == INVALID_SOCKET){
printf("Initializing socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("\nSocket() works");
WSACleanup();
return 0;
}
#include <stdio.h>
#include <winsock2.h>
int main(int argc, char **argv){
WORD wsaVersion = MAKEWORD(2,2);
WSADATA wsaData;
int wsaerr;
wsaerr = WSAStartup(wsaVersion, &wsaData);
if (wsaerr){
printf("WSAStartup failed, exiting.");
return -1;
}
SOCKET somesock;
somesock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (somesock == INVALID_SOCKET){
printf("Socket() failed, exiting.");
WSACleanup();
return -1;
}
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(12344);
if (bind(somesock, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){
printf("Bind() failed, exiting.");
closesocket(somesock);
WSACleanup();
return -1;
}
closesocket(somesock);
WSACleanup();
return 0;
}
Here's are tiny server/client applications. I don't guarrantee they work or are the holy grail of winsock programming, but if they still work then they are good references. ;-)
basicServer.c
/*BasicServer.c*/
#include <stdio.h>
#include <winsock.h>
#include <string.h>
#define BUFFERSIZE 511
int main(int argc, char **argv){
if (argc < 3){
printf("Usage: %s <message to send to client> <ip to listen to>\n", argv[0]);
return -1;
}
SOCKET sock, accepsock = SOCKET_ERROR;
WSADATA wsaData;
WORD wsaVersion = MAKEWORD(2,2);
int sizeOf, wsaerr;
struct sockaddr_in sockinfo, clientinfo;
sockinfo.sin_family = AF_INET;
sockinfo.sin_addr.s_addr = inet_addr(argv[2]);
sockinfo.sin_port = htons(12344);
int sentB, recvB = SOCKET_ERROR;
char sendbuf[BUFFERSIZE];
lstrcpy(sendbuf, argv[1]);
char recvbuf[BUFFERSIZE] = "";
/*WSAStartup(<WORD req_version>, <*WSADATA>)
Initializes Ws2_32.dll, aka the winsock dll.*/
if(WSAStartup(wsaVersion, &wsaData)){
printf("Fatal error: WSAStartup: %d", WSAGetLastError());
return -1;
}
/*socket(<addr family> (AF_INET), <sock type> (SOCK_STREAM/DGRAM), <protocol> (TCPPROTO_IP));
Makes the socket a socket with some info as to what type of socket it should be.*/
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET){
printf("\nFatal error: Socket: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
/*bind(<socket>, <*sockaddr>, sizeof(<sockaddr>));
Need to call this in order to be able to listen() or connect()*/
if(bind(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR){
printf("\nFatal error: Bind: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
/*listen(<socket>, <backlog>);
Puts socket in listening state.*/
if (listen(sock, SOMAXCONN) == SOCKET_ERROR){
printf("\nFatal error: Listen: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("\nDone: Listening for connections.");
sizeOf = sizeof(clientinfo);
while(accepsock == SOCKET_ERROR)
accepsock = accept(sock,(SOCKADDR*)&clientinfo, &sizeOf);
printf("\n %s connected", inet_ntoa(clientinfo.sin_addr));
sock = accepsock;
sentB = send(sock, sendbuf, strlen(sendbuf), 0);
if (sentB == SOCKET_ERROR){
printf("\nServer: Error: Send: %d", WSAGetLastError());
}
printf("\nServer: Sent data:\n Size: %d\n Data: \"%s\"", sentB, sendbuf);
recvB = recv(sock, recvbuf, BUFFERSIZE, 0);
if (recvB < 0){
printf("\nServer: Error: Recv: %d", WSAGetLastError());
}
else if(recvB == 0){
printf("Connection closed.");
closesocket(sock);
WSACleanup();
}
printf("\nServer: Recieved data:\n Size: %d\n Data: \"%s\"", recvB, recvbuf);
printf("\nFinished without errors, shutting down");
shutdown((SOCKET)socket, SD_BOTH);
closesocket(sock);
WSACleanup();
return 0;
}
basicClient.c
/*BasicClient.c*/
#include <stdio.h>
#include <winsock.h>
#include <string.h>
#define BUFFERSIZE 511
int main(int argc, char *argv[]){
if (argc < 3){
printf("Usage: %s <message to send to server> <server IP>\n", argv[0]);
return -1;
}
SOCKET sock, accepsock = SOCKET_ERROR;
WSADATA wsaData;
WORD wsaVersion = MAKEWORD(2,2);
int sizeOf, wsaerr;
struct sockaddr_in sockinfo;
sockinfo.sin_family = AF_INET;
if (argv[2][strlen(argv[2])-1] == '\n')
argv[2][strlen(argv[2])-1] == '\0';
if(isdigit(argv[2][0]))
sockinfo.sin_addr.s_addr = inet_addr(argv[2]);
else return -1;
sockinfo.sin_port = htons(12344);
int sentB, recvB = SOCKET_ERROR;
char sendbuf[BUFFERSIZE];
lstrcpy (sendbuf, argv[1]);
char recvbuf[BUFFERSIZE] = "";
/*WSAStartup(<WORD req_version>, <*WSADATA>)
Initializes Ws2_32.dll, aka the winsock dll.*/
if(WSAStartup(wsaVersion, &wsaData)){
printf("Fatal error: WSAStartup: %d", WSAGetLastError());
return -1;
}
/*socket(<addr family> (AF_INET), <sock type> (SOCK_STREAM/DGRAM), <protocol> (TCPPROTO_IP));
Makes the socket a socket with some info as to what type of socket it should be.*/
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET){
printf("\nFatal error: Socket: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
if(connect(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo))){
printf("\nClient: Fatal error: Connect: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
sentB = send(sock, sendbuf, strlen(sendbuf), 0);
if (sentB == SOCKET_ERROR){
printf("\nClient: Fatal error: Send: %d", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("\nClient: Sent data:\n Size: %d\n Data: \"%s\"", sentB, sendbuf);
recvB = recv(sock, recvbuf, BUFFERSIZE, 0);
if (recvB < 0){
printf("\nClient: Error: Recv: %d", WSAGetLastError());
}
else if(recvB == 0){
printf("Connection closed.");
closesocket(sock);
WSACleanup();
}
printf("\nClient: Recieved data:\n Size: %d\n Data: \"%s\"", recvB, recvbuf);
printf("\nFinished without errors, shutting down");
shutdown((SOCKET)socket, SD_BOTH);
closesocket(sock);
WSACleanup();
return 0;
}
Don't forget to link with the winsocket library! (in MinGW, that's ws2_32.a, pass the parameter: -lws2_32 when compiling)
Salem commented: Looks good. +26
zedame 0 Newbie Poster
I agree with clockowl.
Anyway, when you say you were asked....by who your lecturer.
for your info i'm working with Java technology for about three years but now i have to stick with c++ for some issues so instead of running blind i was asking for guidance...i need to know the basic idea of how to implement applications like IM in C++...i'm not askin for "Gimme some code plz" thing...
Freaky_Chris 299 Master Poster
The concept is exactly the same, server/client or client/client it really is upto you. Lets say you go for server/client the more likely. Y9ou have the server set up to listen for connections and spawn new threads to deal with no connections recieved and forward data from an incoming socket to another outbound socket. The client will send a socket connection request to the servers IP address once connect is then free to recieve and send information as desired.
Chris
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.