![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
problem with network program
im created a simple chat program, but it doesnt send or receive messages very well, and you have to send a message before you can receive the one that was sent to you. does anyone have any suggestions?
here's the code for the server: #include<winsock.h>
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAX_CLIENTS 30
int net;
int client_count=0;
ofstream outfile;
char data[50];
char rdata[50];
SOCKET server;
SOCKET client;
int main()
{
WORD sockversion;
WSADATA wsaData;
sockversion=MAKEWORD(1,1);
WSAStartup(sockversion,&wsaData);
if((server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
{
sockaddr_in sin;
sin.sin_family=PF_INET;
sin.sin_port=htons(14567);
sin.sin_addr.s_addr=INADDR_ANY;
if((net=bind(server,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
{
if((net=listen(server,MAX_CLIENTS))!=SOCKET_ERROR)
{
client_count++;
cout<<"client found\n";
if(client_count<=MAX_CLIENTS)
{
if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
{
cout<<client_count<<'\n';
do
{
cout<<"enter message: ";
cin.get(data,49);
cin.ignore(80,'\n');
if((net=send(server,data,sizeof(data),NULL))!=SOCKET_ERROR || (net=recv(client,rdata,49,NULL))!=SOCKET_ERROR)
{
if(strlen(rdata)==NULL)
{
cout<<" ";
}
else
{
cout<<rdata<<'\n';
}
}
}while(client_count!>MAX_CLIENTS);
}
else
{
cout<<"failed accept\n";
WSACleanup();
closesocket(server);
closesocket(client);
}
}
else
{
cout<<"server cant handle anymore clients";
WSACleanup();
closesocket(server);
closesocket(client);
}
}
else
{
client_count--;
cout<<"failed listen\n";
WSACleanup();
closesocket(server);
closesocket(client);
}
}
else
{
cout<<"failed bind\n";
WSACleanup();
closesocket(server);
closesocket(client);
}
}
else
{
cout<<"invalid socket\n";
WSACleanup();
closesocket(server);
closesocket(client);
}
return(client_count);
}and here's the client: #include<winsock.h>
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
int net;
char data[50];
char rdata[50];
ofstream outfile;
SOCKET server;
SOCKET client;
int main()
{
WORD sockversion;
WSADATA wsaData;
sockversion=MAKEWORD(1,1);
WSAStartup(sockversion,&wsaData);
if((client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
{
sockaddr_in sin;
sin.sin_family=PF_INET;
sin.sin_port=htons(14567);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
sockaddr_in from;
int fromlen=sizeof(from);
if((net=connect(client,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
{
cout<<"<connected to "<<inet_ntoa(from.sin_addr)<<">"<<'\n';
cout<<"<on port "<<"8888"<<">"<<'\n';
do
{
cout<<"enter a message ";
cin.get(data,49);
cin.ignore(80,'\n');
cout<<rdata<<'\n';
if((net=send(client,data,sizeof(data),NULL))!=SOCKET_ERROR || (net=recv(server,rdata,49,NULL))!=SOCKET_ERROR)
{
if(strlen(rdata)==NULL)
{
cout<<" ";
}
else
{
cout<<rdata<<'\n';
}
}
}while(!NULL);
}
else
{
cout<<"failed to connect to server\n";
WSACleanup();
closesocket(client);
closesocket(server);
}
}
else
{
cout<<"invalid socket\n";
WSACleanup();
closesocket(client);
closesocket(server);
}
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
You'll probably have to use threads.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
what the heck is that?
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 144
Rep Power: 0
![]() |
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#5 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
thanks
|
|
|
|
|
|
#6 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
how would i use the select() function in these programs
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
for Windows> check msdn.microsoft.com
for *nix...http://www.scit.wlv.ac.uk/~jphb/com...ets.html#select
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#8 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
is it best to use TCP or UDP for a chat program, and could someone recomend a website with a tutorial or something.
|
|
|
|
|
|
#9 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4
![]() |
could someone show me how to do some simple multithreading
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() ![]() ![]() |
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|