Client/server fejl i cpp
Jeg har lige læst Grundlæggende Programmering i C++ (Globe), hvor forfatteren som eksempel laver flg. simple chatprogram.Det er sammensat af flere includes, som jeg håber at jeg har samlet rigtigt.
Her er koden:
Server:
#include <winsock2.h>
#include <iostream>
using namespace std;
class Server
{
public:
void StartServer(int port);
void Luk();
private:
int os,ns;
sockaddr_in saddr;
int clength, res, len;
WSADATA wlib;
char txt[100];
};
void Server::StartServer(int port)
{
WSAStartup(0x0101, &wlib);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port); // PORT NO.
saddr.sin_addr.s_addr = INADDR_ANY;
clength = sizeof(saddr);
if((os = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))!=-1)
{
cout<<"Socket skabt"<<endl;
}
else
{
cout<<"Kunne ikke skabe socket"<<endl;
Luk();
}
if((res = bind(os, (sockaddr *) &saddr, clength))!=-1)
{
cout<<"Bind OK"<<endl;
}
else
{
cout<<"Fejl i Bind(): Check portnumrene"<<endl;
Luk();
}
if( (res = listen(os,5)) !=-1)
{ // backlog
cout<<"Venter på en klient.."<<endl;
}
else
{
cout<<"Fejl i Listen"<<endl;
Luk();
}
while(1)
{
ns = accept(os, (sockaddr *)&saddr, &clength);
while((len = recv(ns, txt, sizeof(txt),0))!=0 )
{
cout <<"Data modtaget: " << txt<<endl;
}
closesocket(ns);
}
Luk();
}
void Server::Luk()
{
closesocket(os);
WSACleanup();
}
int main()
{
Server s;
s.StartServer(7777);
return 0;
}
Client:
#include<iostream>
#include <winsock2.h>
using namespace std;
class Klient
{
public:
void Connect(int port, char *adr);
void SendMsg(char *pmsg);
void CloseCon();
private:
int fd;
sockaddr_in saddr;
WSADATA wlib;
int nOk;
};
void Klient::Connect(int port, char *adr)
{
WSAStartup(0x0101, &wlib);
fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr(adr);
connect(fd, (sockaddr *) &saddr, sizeof(saddr));
}
void Klient::SendMsg(char *pmsg)
{
if( (nOk=send(fd, pmsg,100, 0)) == -1)
{
cout<<"Kunne ikke sende!"<<endl;
exit(1);
}
}
void Klient::CloseCon()
{
closesocket(fd);
WSACleanup();
}
char msg[100];
char stop;
int main()
{
Klient k;
k.Connect(7777,"127.0.0.1");
do
{
cout<<"Send data til serveren:"<<endl;
cin>>msg;
k.SendMsg(msg);
cout<<"Tast n for at stoppe"<<endl;
cin>>stop;
}
while(stop!='n');
k.CloseCon();
return 0;
}
Ved kompileringen af begge filer siger den:
.objs\main.o:main.cpp:(.text+0x119): undefined reference to `WSAStartup@8'
.objs\main.o:main.cpp:(.text+0x13b): undefined reference to `socket@12'
.objs\main.o:main.cpp:(.text+0x15a): undefined reference to `htons@4'
.objs\main.o:main.cpp:(.text+0x16f): undefined reference to `inet_addr@4'
.objs\main.o:main.cpp:(.text+0x194): undefined reference to `connect@12'
.objs\main.o:main.cpp:(.text+0x1ca): undefined reference to `send@16'
.objs\main.o:main.cpp:(.text+0x227): undefined reference to `closesocket@4'
.objs\main.o:main.cpp:(.text+0x22f): undefined reference to `WSACleanup@0'
Fejlen er sikkert ret basal, noget med linker ell lign.
Jeg bruger Code::Blocks med GNU GCC comp.