![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
thanks
|
|
|
|
|
|
#12 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
i have another problem....i can only send and recv 1 message. whats up with that. im thinking its probably because the char array is still filled with data, but im not sure. here is the code:
server.cpp #include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500
//functions
void send_recv();
//global variables
short int port;
int connected=false;
int sent=false;
int received=false;
int client_connected=false;
char rdata[MAXBUFLEN];
char data[500];
//socket stuff
SOCKET client;
SOCKET server;
WORD sockversion;
WSADATA wsaData;
int main(void)
{
if((sockversion=WSAStartup(0x0101,&wsaData))!=SOCKET_ERROR)
{
if((server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
{
sockaddr_in from;
int fromlen=sizeof(from);
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
if((bind(server,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
{
if((listen(server,4))!=SOCKET_ERROR)
{
if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
{
client_connected=true;
connected=true;
cout<<"\n";
cout<<"-----------------------\n";
cout<<"welcome to Network Chat\n";
cout<<"-----------------------\n";
cout<<"-hlp for help\n";
cout<<"\n";
while(true)
{
char escape[5]="-q";
char ip_addr[5]="-ip";
char clear[5]="-clr";
char help[5]="-hlp";
char disconn[5]="-dis";
char conn[5]="-con";
char status[6]="-stat";
cout<<">> ";
cin.get(data,sizeof(data));
cin.ignore(80,'\n');
if(strcmp(data,escape)==0)
{
WSACleanup();
closesocket(client);
return 0;
}
else if(strcmp(data,ip_addr)==0)
{
cout<<"\n";
cout<<"<your IP: "<<inet_ntoa(sin.sin_addr)<<"> "<<'\n';
cout<<"<your port: "<<htons(sin.sin_port)<<">"<<'\n';
cout<<"\n";
cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
cout<<"<server port: "<<htons(from.sin_port)<<"> "<<'\n';
cout<<"\n";
}
else if(strcmp(data,clear)==0)
{
system("cls");
}
else if(strcmp(data,help)==0)
{
cout<<"\n";
cout<<"-q quit\n";
cout<<"-clr clear screen\n";
cout<<"-ip server IP and port #\n";
cout<<"-dis disconnect from server\n";
cout<<"-stat connection status( 1 = connected, 0 = not connected)\n";
cout<<"\n";
}
else if(strcmp(data,disconn)==0)
{
cout<<"\n";
closesocket(server);
closesocket(client);
WSACleanup();
connected=false;
cout<<"you have disconnected\n";
cout<<"\n";
}
else if(strcmp(data,status)==0)
{
cout<<"\n";
cout<<"<connection status: "<<connected<<" >"<<'\n';
cout<<"\n";
}
else
{
send_recv();
}
}
}
else
{
connected=false;
cout<<"failed accept\n";
closesocket(server);
closesocket(client);
WSACleanup();
}
}
else
{
connected=false;
cout<<"failed bind\n";
closesocket(server);
closesocket(client);
WSACleanup();
}
}
else
{
connected=false;
cout<<"failed bind\n";
closesocket(server);
closesocket(client);
WSACleanup();
}
}
else
{
connected=false;
cout<<"invalid socket\n";
WSACleanup();
return 0;
}
}
else
{
cout<<"WSAStartup() failed\n";
WSACleanup();
return 0;
}
}
void send_recv()
{
if((recv(client,rdata,MAXBUFLEN-1,0))==SOCKET_ERROR)
{
received=false;
int error=WSAGetLastError();
if(error==WSAENOTSOCK)
{
cout<<"not a valid socket\n";
WSACleanup();
}
else if(error==WSAENOTCONN)
{
cout<<"socket not connected\n";
WSACleanup();
}
cout<<"failed recv\n";
WSACleanup();
}
else
{
received=true;
cout<<"client says: "<<rdata<<'\n';
}
if((send(client,data,sizeof(data),0))==SOCKET_ERROR)
{
sent=false;
cout<<"failed send\n";
WSACleanup();
}
else
{
sent=true;
}
int addr_len;
addr_len=sizeof(struct sockaddr*);
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
closesocket(server);
closesocket(client);
connected=false;
if((listen(server,4))!=SOCKET_ERROR)
{
sockaddr_in acc;
acc.sin_family=AF_INET;
acc.sin_port=htons(80);
acc.sin_addr.s_addr=inet_addr("127.0.0.1");
if((client=accept(server,NULL,NULL))!=SOCKET_ERROR)
{
connected=true;
}
else
{
cout<<"\n";
cout<<"failed accept\n";
cout<<"\n";
connected=false;
}
}
else
{
cout<<"\n";
cout<<"failed listen\n";
cout<<"\n";
connected=false;
}
}client.cpp #include<winsock.h>
#include<windows.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#pragma comment(lib,"wsock32.lib")
#define MAXBUFLEN 500
void send_recv();
short int port;
char rdata[MAXBUFLEN];
char data[500];
SOCKET client;
SOCKET server;
int connected=false;
int sent=false;
int received=false;
int main(void)
{
WORD sockversion;
WSADATA wsaData;
if((sockversion=WSAStartup(0x0101,&wsaData))!=SOCKET_ERROR)
{
if((client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET)
{
sockaddr_in from;
int fromlen=sizeof(from);
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(client,(LPSOCKADDR)&sin,sizeof(sin))!=SOCKET_ERROR)
{
connected=true;
cout<<"\n";
cout<<"-----------------------\n";
cout<<"welcome to Network Chat\n";
cout<<"-----------------------\n";
cout<<"-hlp for help\n";
cout<<"\n";
while(true)
{
char escape[5]="-q";
char ip_addr[5]="-ip";
char clear[5]="-clr";
char help[5]="-hlp";
char disconn[5]="-dis";
char conn[5]="-con";
char status[6]="-stat";
cout<<">> ";
cin.get(data,sizeof(data));
cin.ignore(80,'\n');
if(strcmp(data,escape)==0)
{
WSACleanup();
closesocket(client);
return 0;
}
else if(strcmp(data,ip_addr)==0)
{
cout<<"\n";
cout<<"<your IP: "<<inet_ntoa(sin.sin_addr)<<"> "<<'\n';
cout<<"<your port: "<<htons(sin.sin_port)<<">"<<'\n';
cout<<"\n";
cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n';
cout<<"<server port: "<<htons(from.sin_port)<<"> "<<'\n';
cout<<"\n";
}
else if(strcmp(data,clear)==0)
{
system("cls");
}
else if(strcmp(data,help)==0)
{
cout<<"\n";
cout<<"-q quit\n";
cout<<"-clr clear screen\n";
cout<<"-ip server IP and port #\n";
cout<<"-dis disconnect from server\n";
cout<<"-con connect to server\n";
cout<<"-stat connection status( 1 = connected, 0 = not connected)\n";
cout<<"\n";
}
else if(strcmp(data,disconn)==0)
{
cout<<"\n";
closesocket(server);
closesocket(client);
WSACleanup();
connected=false;
cout<<"you have disconnected\n";
cout<<"\n";
}
else if(strcmp(data,conn)==0)
{
cout<<"\n";
connect(client,(LPSOCKADDR)&sin,sizeof(sin));
connected=true;
cout<<"connected to server\n";
cout<<"\n";
}
else if(strcmp(data,status)==0)
{
cout<<"\n";
cout<<"<connection status: "<<connected<<" >"<<'\n';
cout<<"\n";
}
else
{
send_recv();
}
}
}
else
{
connected=false;
cout<<"failed to connect to server\n";
closesocket(client);
WSACleanup();
}
}
else
{
connected=false;
cout<<"invalid socket\n";
WSACleanup();
return 0;
}
}
else
{
cout<<"WSAStartup() failed\n";
WSACleanup();
return 0;
}
}
void send_recv()
{
if((send(client,data,sizeof(data),0))==SOCKET_ERROR)
{
sent=false;
cout<<"failed send\n";
WSACleanup();
}
else
{
sent=true;
}
int addr_len;
addr_len=sizeof(struct sockaddr*);
if((recv(client,rdata,MAXBUFLEN-1,0))==SOCKET_ERROR)
{
received=false;
int error=WSAGetLastError();
if(error==WSAENOTSOCK)
{
cout<<"not a valid socket\n";
WSACleanup();
}
else if(error==WSAENOTCONN)
{
cout<<"socket not connected\n";
WSACleanup();
}
cout<<"failed recv\n";
WSACleanup();
}
else
{
received=true;
cout<<"server says: "<<rdata<<'\n';
}
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
closesocket(server);
closesocket(client);
WSACleanup();
connected=false;
if((connect(client,(LPSOCKADDR)&sin,sizeof(sin)))!=SOCKET_ERROR)
{
connected=true;
}
} |
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Recommend you reduce your tab size. It's hellacious trying to keep a view of the code having to move up and down to look and then get at the horizontal scroll bar. Not having seen all the actual code, I'll just say that you don't "empty" the buffer, it just gets copied to the transmission medium, say, and then you write over it with new stuff. Data isn't going to refuse to write to it because there's something in it -- there's always something in it, if just uninitialized junk.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#14 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
sorry...ill try to keep the tags smaller. so what could be the problem with the program then?
|
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, as I say, I couldn't assimilate it. I'll copy and paste it and run it thorough my C beautifier and take a look. It isn't the tags, it's the tabs. They're apparently 8 spaces, which makes for hellacious indentation, sometimes. I sometimes use 4 and sometimes use 3 because I'm wishy-washy and can't make up my mind.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#16 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
oh ok....thanks a bunch
|
|
|
|
|
|
#17 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
In my text editor the tabs are only 4 spaces, but probably when I use those tags it mess's it all up.
|
|
|
|
|
|
#18 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It isn't a biggie, guy, I can deal with it. Some editors let you choose whether to leave the tabs or convert them to spaces. If you convert them to spaces, that reproduces 'bout anywhere. If you leave them tabs, it's at the discretion of the reproducing app (in this case, these pages), which might assign any number of spaces to them.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#19 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
First, just to get the program ready to compile, I made the listed changes:
Quote:
#include<winsock2.h> #include<windows.h> #include<iostream> #include<cstdlib> #include<cstdio> #include<fstream> //#pragma comment(lib,"wsock32.lib") using namespace std; cout<<"<server IP: "<<inet_ntoa(from.sin_addr)<<"> "<<'\n'; I'll continue to look at the functionality of the code, thought I'd just drop the results of the initial whack on you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#20 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 246
Rep Power: 4
![]() |
hey thanks...its helped alot. As for the style, thats the way I was taught, probably not the best habit.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|