![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
problem with network application
whenever i make a network program it always says "failed recv()" or "failed send()", and i can never get it to work. any suggestions?
here's the server: #include<winsock.h>
#include<iostream.h>
#include<string.h>
#pragma comment(lib,"wsock32.lib")
SOCKET server;
SOCKET client;
int main()
{
WORD sockversion;
WSADATA wsaData;
int net;
sockversion=MAKEWORD(1,1);
WSAStartup(sockversion,&wsaData);
server=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(server==INVALID_SOCKET)
{
cout<<"invalid socket\n";
WSACleanup();
}
sockaddr_in sin;
sin.sin_family=PF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=INADDR_ANY;
net=bind(server,(LPSOCKADDR)&sin,sizeof(sin));
if(net==SOCKET_ERROR)
{
cout<<"failed bind()";
WSACleanup();
}
net=listen(server,4);
if(net==SOCKET_ERROR)
{
cout<<"failed listen\n";
WSACleanup();
}
client=accept(server,NULL,NULL);
if(client==INVALID_SOCKET)
{
cout<<"failed accept\n";
WSACleanup();
}
while(true);
{
char rdata[5000];
char data[5000];
net=recv(client,rdata,strlen(rdata),0);
if(net==SOCKET_ERROR)
{
cout<<"failed recv()\n";
WSACleanup();
}
cout<<"===================================\n";
cout<<"\n";
cout<<">>"<<rdata<<'\n';
cout<<"<you said>"<<data<<'\n';
cout<<"\n";
cout<<"===================================\n";
cout<<">> ";
cin.get(data,4999);
cin.ignore(80,'\n');
net=send(server,data,strlen(data),0);
if(net==SOCKET_ERROR)
{
cout<<"failed send\n";
WSACleanup();
}
}
WSACleanup();
closesocket(server);
closesocket(client);
return 0;
}here's the client #include<winsock.h>
#include<iostream.h>
#include<string.h>
#pragma comment(lib,"wsock32.lib")
SOCKET server;
SOCKET client;
int main()
{
WORD sockversion;
WSADATA wsaData;
int net;
sockversion=MAKEWORD(1,1);
WSAStartup(sockversion,&wsaData);
client=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(client==INVALID_SOCKET)
{
cout<<"invalid socket\n";
WSACleanup();
}
sockaddr_in sin;
sin.sin_familt=PF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr("127.0.0.1");
net=connect(client,(LPSOCKADDR)&sin,sizeof(sin));
if(net==SOCKET_ERROR)
{
cout<<"failed to connect to server\n";
WSACleanup();
}
while(true);
{
char rdata[5000];
char data[5000];
net=send(server,data,strlen(data),0);
if(net==SOCKET_ERROR)
{
cout<<"failed send\n";
WSACleanup();
}
cout<<"===================================\n";
cout<<"\n";
cout<<">>"<<rdata<<'\n';
cout<<"<you said>"<<data<<'\n';
cout<<"\n";
cout<<"===================================\n";
cout<<">> ";
cin.get(data,4999);
cin.ignore(80,'\n');
net=recv(client,rdata,strlen(rdata),0);
if(net==SOCKET_ERROR)
{
cout<<"failed recv()\n";
WSACleanup();
}
}
WSACleanup();
closesocket(server);
closesocket(client);
return 0;
}thanks in advance Last edited by Mjordan2nd; Jul 1st, 2005 at 8:53 PM. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
These are HTML pages; they eat whitespace. Please put your code in tags to preserve its indentation. Otherwise, it's uglier than my ex-mother-in-law, and I won't read it. The "How to Post...." thread in the C forum is a good read, also.
__________________
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 |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Code tags added for ease on the eyes.
__________________
"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 |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 816
Rep Power: 4
![]() |
In this line in the server
net=recv(client,rdata,strlen(rdata),0); In this line in the client net=send(server,data,strlen(data),0); You need to either send the zero byte from the client to the server, to indicate end of string, or use the received length to add a zero byte to the end of the characters you are putting in rdata on the server, otherwise your string is unterminated. |
|
|
|
|
|
#5 | |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
Could someone tell me how to use the "select() function", or how to implement it in my program?
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Go here in Beej's guide. I recommend you bookmark this or download a copy of the PDF version or something. Google is your friend if Beej falls through.
__________________
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|