Archived

This topic is now archived and is closed to further replies.

TheTempest

Sample SourceCode....

Recommended Posts

Hello again, Does anyone have some sample Winsock Win32 source code for a Chat server and a Chat client...or any references to help me create one. I''ve done it in dos (console), but win32...hehe, such a pain in the arse, but sooo worth it. Thanks for any helpfull suggestions.

Share this post


Link to post
Share on other sites
http://code.jakobbieling.de/

with CNetworking I wrote this program

----------------------
multi-user chat server
----------------------
main.cpp
//steve dobbs is brill
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "CConnection.h"
#include "ChatServer.h"

int main(){
char key;
ChatServer chat(50,7778); //maximun of 50 clients, ;istening on port 7778

while(key!=''q''){
if(kbhit()) key=getch();
chat.ServerUpdate();
}
return 0;
}

---------------------
ChatServer.h
#define BUFFSIZE 150

#ifndef NETCONST
#include "netconst.h"
#define NETCONST
#endif

#define CHATSERVER

//______________________________________

class conDat{
public:
void clear_rcv(){
for(int i=0;i receive=NULL;
}
rp=0;
}
void clear_snd(){
for(int i=0;i receive[i]=NULL;
}
sp=0;
}

char *receive;
int rcv_buffersize,snd_buffersize,rp,sp,flag;
char *send;
};

//___________________________________________________________________________________

//steve dobbs is super
class ChatServer{
public:
ChatServer(int ncl,int port); //number of clients, port
~ChatServer();

void ServerUpdate();
void BcastExcluding(int clientID,char *buff,int size);

private:

conDat *clientData; // each client has its data space so processing can be done interleved.

CNetworking net;
CConnection **client; //and array of pointers to different CConnection classe''s
int *flagArray; //0 if element is untaken, 1 if slot is taken
int nclients;
};

void ChatServer::BcastExcluding(int clientID,char *buff,int size){
for(int i=0;i if((flagArray[i]==1)&&(i!=clientID)){
client[i]->Send(buff,size);
}
}
}

ChatServer::ChatServer(int ncl, int port){
nclients=ncl;
net.Listen(port);
CConnection *c;

int i;
//CConnection *con;
//con=new CConnection[ncl];
void *con; con=malloc(sizeof(void *));
client=(CConnection **)con;
clientData=(conDat *)malloc(BUFFSIZE*sizeof(conDat));

flagArray=(int *)malloc(sizeof(int)*nclients);
for(i=0;i clientData[i].receive=(char *)malloc(BUFFSIZE*sizeof(char));
clientData[i].send=(char *)malloc(BUFFSIZE*sizeof(char));
clientData[i].rcv_buffersize=BUFFSIZE;
clientData[i].snd_buffersize=BUFFSIZE;
clientData[i].flag=0;
clientData[i].rp=0;
clientData[i].sp=0;
flagArray[i]=0;// set array
}

}


void ChatServer::ServerUpdate(){
//must be updated from the main loop
int i,sel=-1;

if(net.HasAccepted()>0){ // there are connections on dah stack
//find a free connection
for(i=0;i if(flagArray[i]==0){
sel=i; break;
}
}

//now during this cycle, create a connection/socket
if(sel!=-1){ //do the connection thing
flagArray[sel]=1;
client[sel]=net.GetAccepted();
printf("hohoho, client on slot %d has connected to server\n",i);
}

}

int nbytes=0;
char byte;
//all active connections now to be updated
for(i=0;i if(flagArray[i]==1){ //
nbytes=client[i]->HasReceived();
if(nbytes>0){
client[i]->Receive(&byte,1);
clientData[i].receive[clientData[i].rp]=byte;
clientData[i].rp++;
if(byte==NULL){
printf("%s\n",clientData[i].receive);

BcastExcluding(i,clientData[i].receive,clientData[i].rp);
//that line broadcasts to all clients except current, what is in the receive buffer.
//ie the text that was sent to the server by said client.

clientData[i].clear_rcv();

while(client[i]->HasReceived()>0){


byte=client[i]->Receive(&byte,1); //whittle down excess data;
}
}
}
}

//go look for more active clients
}


}

ChatServer::~ChatServer(){
free(flagArray);
net.StopListen();
}
---------------------------------------------
netconst.h
//wibbily wobbily
#define NETCONST

#define netstring 0
#define netint 1
#define netfloat 2
#define netdouble 3
#define netbin 4
---------------------------------------------

______________________________
chat client
______________________________
main.cpp
//biggily bop!
#include "CConnection.h"
#include <stdio.h>
#include <conio.h>


int main(){
CNetworking net;
CConnection con;

char temp[500],name[128]="",key=''_'';

bool success=net.GetLocalName(name,128);

char myip[20] = ""; // storage for one ip ("xxx.xxx.xxx.xxx")
success = net.GetLocalIP(myip, 15);

char testText[100]="wibbily wobbly biggily bop";
char buff[256];
int i;

//------------------
int newline=1,p=0;
i=0;
//------------------
char byte,buffrc[256];
int nbytes;
printf("enter chatserver IP address >");
scanf("%s",myip);
printf("%s\n",myip);
if(con.Connect(myip,7778)){
printf("Connection Made :-)\n");

while(key!=27 /*ESC*/){
if(kbhit()) {
key=getch();
//con.Send(testText,100);
if((key!=13) && (i<256)){
printf("%c",key);
newline=0;
buff[i]=key; i++;
}
else{
key=0;
buff[i]=NULL; i++;
con.Send(buff,i);
printf("\n");
i=0;
}
}

//here look for data received
/*
if(ok==1){
nbytes=c[0]->HasReceived();
if(nbytes>0){
c[0]->Receive(name,1);
buff[i]=name[0]; i++;
if(name[0]==NULL){
printf("%s\n",buff);
i=0;
c[0]->Receive(temp,c[0]->HasReceived());
}
}

*/
nbytes=con.HasReceived();
if(nbytes>0){
con.Receive(&byte,1);
buffrc[p]=byte;p++;
if(byte==NULL){
printf("%s\n",buffrc);
p=0;
con.Receive(temp,con.HasReceived()); //empty buffer;
}
}
}
}
else
printf("No Connection possible :-(\n");


con.Disconnect();
printf("bye\n");

return 0;
}
--------------------------
using visual c++
both are separate projects and in "sources", main.cpp and CConnection.cpp are added.

CConnection.cpp
CConnection.h from the website above which has good documentation.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I do believe the original poster requested Win32 (as in windows) source, and specifically not console source as he had already done that!

Share this post


Link to post
Share on other sites