Sample SourceCode....
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.
"There are no such things as stupid questions...Just stupid people :)"-Me
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=NULL;<br> }<br> sp=0;<br> }<br><br> char *receive;<br> int rcv_buffersize,snd_buffersize,rp,sp,flag;<br> char *send;<br>};<br><br>//___________________________________________________________________________________<br><br>//steve dobbs is super<br>class ChatServer{<br>public:<br> ChatServer(int ncl,int port); //number of clients, port<br> ~ChatServer();<br><br> void ServerUpdate();<br> void BcastExcluding(int clientID,char *buff,int size);<br><br>private:<br> <br> conDat *clientData; // each client has its data space so processing can be done interleved.<br><br> CNetworking net;<br> CConnection **client; //and array of pointers to different CConnection classe''s<br> int *flagArray; //0 if element is untaken, 1 if slot is taken<br> int nclients;<br>};<br><br>void ChatServer::BcastExcluding(int clientID,char *buff,int size){<br> for(int i=0;i<nclients;i++){<br> if((flagArray==1)&&(i!=clientID)){<br> client->Send(buff,size);<br> }<br> }<br>}<br><br>ChatServer::ChatServer(int ncl, int port){<br> nclients=ncl;<br> net.Listen(port);<br> CConnection *c;<br><br> int i;<br> //CConnection *con;<br> //con=new CConnection[ncl];<br> void *con; con=malloc(sizeof(void *));<br> client=(CConnection **)con;<br> clientData=(conDat *)malloc(BUFFSIZE*sizeof(conDat));<br><br> flagArray=(int *)malloc(sizeof(int)*nclients);<br> for(i=0;i<nclients;i++){<br> clientData.receive=(char *)malloc(BUFFSIZE*sizeof(char));<br> clientData.send=(char *)malloc(BUFFSIZE*sizeof(char));<br> clientData.rcv_buffersize=BUFFSIZE;<br> clientData.snd_buffersize=BUFFSIZE;<br> clientData.flag=0;<br> clientData.rp=0;<br> clientData.sp=0;<br> flagArray=0;// set array<br> }<br><br>}<br><br><br>void ChatServer::ServerUpdate(){<br> //must be updated from the main loop<br> int i,sel=-1;<br> <br> if(net.HasAccepted()>0){ // there are connections on dah stack<br> //find a free connection<br> for(i=0;i<nclients;i++){<br> if(flagArray==0){<br> sel=i; break;<br> }<br> }<br><br> //now during this cycle, create a connection/socket<br> if(sel!=-1){ //do the connection thing <br> flagArray[sel]=1;<br> client[sel]=net.GetAccepted();<br> printf("hohoho, client on slot %d has connected to server\n",i);<br> }<br><br> }<br><br> int nbytes=0;<br> char byte;<br> //all active connections now to be updated<br> for(i=0;i<nclients;i++){<br> if(flagArray==1){ //<br> nbytes=client->HasReceived();<br> if(nbytes>0){<br> client->Receive(&byte,1);<br> clientData.receive[clientData.rp]=byte;<br> clientData.rp++;<br> if(byte==NULL){<br> printf("%s\n",clientData.receive);<br><br> BcastExcluding(i,clientData.receive,clientData.rp);<br> //that line broadcasts to all clients except current, what is in the receive buffer.<br> //ie the text that was sent to the server by said client.<br> <br> clientData.clear_rcv();<br> <br> while(client->HasReceived()>0){<br> <br><br> byte=client->Receive(&byte,1); //whittle down excess data;<br> }<br> }<br> }<br> }<br><br> //go look for more active clients<br> }<br><br> <br>}<br><br>ChatServer::~ChatServer(){<br> free(flagArray);<br> net.StopListen();<br>}<br>βββββββββββββββ<br>netconst.h<br>//wibbily wobbily<br>#define NETCONST<br><br>#define netstring 0<br>#define netint 1<br>#define netfloat 2<br>#define netdouble 3<br>#define netbin 4<br>βββββββββββββββ<br><br>______________________________<br>chat client<br>______________________________<br>main.cpp<br>//biggily bop!<br>#include "CConnection.h"<br>#include <stdio.h><br>#include <conio.h><br><br><br>int main(){<br> CNetworking net;<br> CConnection con;<br> <br> char temp[500],name[128]="",key=''_'';<br><br> bool success=net.GetLocalName(name,128);<br> <br> char myip[20] = ""; // storage for one ip ("xxx.xxx.xxx.xxx")<br> success = net.GetLocalIP(myip, 15);<br> <br> char testText[100]="wibbily wobbly biggily bop";<br> char buff[256];<br> int i;<br> <br> //ββββββ<br> int newline=1,p=0;<br> i=0;<br> //ββββββ<br> char byte,buffrc[256];<br> int nbytes;<br> printf("enter chatserver IP address >");<br> scanf("%s",myip);<br> printf("%s\n",myip);<br> if(con.Connect(myip,7778)){<br> printf("Connection Made :-)\n");<br><br> while(key!=27 /*ESC*/){<br> if(kbhit()) {<br> key=getch();<br> //con.Send(testText,100);<br> if((key!=13) && (i<256)){<br> printf("%c",key);<br> newline=0;<br> buff=key; i++;<br> }<br> else{<br> key=0;<br> buff=NULL; i++;<br> con.Send(buff,i);<br> printf("\n");<br> i=0;<br> }<br> }<br><br> //here look for data received<br> /*<br> if(ok==1){<br> nbytes=c[0]->HasReceived();<br> if(nbytes>0){<br> c[0]->Receive(name,1);<br> buff=name[0]; i++;<br> if(name[0]==NULL){ <br> printf("%s\n",buff);<br> i=0;<br> c[0]->Receive(temp,c[0]->HasReceived());<br> }<br> }<br> <br> */<br> nbytes=con.HasReceived();<br> if(nbytes>0){<br> con.Receive(&byte,1);<br> buffrc[p]=byte;p++;<br> if(byte==NULL){<br> printf("%s\n",buffrc);<br> p=0;<br> con.Receive(temp,con.HasReceived()); //empty buffer;<br> }<br> }<br> }<br> }<br> else<br> printf("No Connection possible :-(\n");<br><br> <br> con.Disconnect();<br> printf("bye\n");<br><br> return 0;<br>}<br>βββββββββ<br>using visual c++<br>both are separate projects and in "sources", main.cpp and CConnection.cpp are added.<br><br>CConnection.cpp<br>CConnection.h from the website above which has good documentation.<br><br> </i>
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
}
rp=0;
}
void clear_snd(){
for(int i=0;i
eek, its went into itallics. hopefully sense can still be made of it. i dont know how to post source code.
Use the *source* and */source* tags (replace the *''s with square braces).
![](wink.gif)
SysOp_1101
August 13, 2002 12:48 AM
I do believe the original poster requested Win32 (as in windows) source, and specifically not console source as he had already done that!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement