Sign in to follow this  
Joesavage1

Winsock Problems..

Recommended Posts

Joesavage1    100
Hi, I'm sure you probably get this a lot, but I've got some problems with a WinSock chat application I made with aid from this video: http://www.youtube.com/watch?v=AVyKZeOZnzk&feature=related (and part one). I've followed the steps exactly, and I'm starting to figure out how it all works; and why it all works, however I get tonnes of errors when trying to debug it. Here is my code.. ChatProgram1.cpp (equivalent to main.cpp): #include <iostream> #include "Socket.h" using namespace std; const int STRLEN = 256; int main(){ int choice; int port = 666; char *ipAddress = "127.0.0.1"; char recMessage[STRLEN]; char sendMessage[STRLEN]; bool done = false; cout << "1)Host server" << endl; cout << "2)Join server" << endl; cout << "3)Quit" << endl; if(choice==3){ exit(0); } else if(choice==2){ //Client ClientSocket sockClient; cout << "Attempting to connect.." << endl; sockClient.ConnectToServer(ipAddress, port); //Connected while(!done){ cout << "Enter message " << endl; cin >> sendMessage; cout << "Client > " << sendMessage << endl; sockClient.SendData(sendMessage); cout << "--MESSAGE SENT--" << endl; cout << "Waiting for message.." << endl; sockClient.ReceiveData(recMessage, STRLEN); cout << "Server > " << recMessage << endl; if((strcmp(recMessage, "end") == 0) || (strcmp(sendMessage, "end") == 0)){ done = true; } } sockClient.CloseConnection(); } else if(choice==1){ //Server ServerSocket sockServer; cout << "Hosting.." << endl; sockServer.StartHosting(port); sockServer.Listen(); //Connected while(!done){ cout << "Waiting for message.." << endl; sockServer.ReceiveData(recMessage, STRLEN); cout << "Client > " << recMessage << endl; cout << "/nEnter message " << endl; cin >> sendMessage; cout << "Server > " << sendMessage << endl; sockServer.SendData(sendMessage); cout << "--MESSAGE SENT--" << endl; if((strcmp(recMessage, "end") == 0) || (strcmp(sendMessage, "end") == 0)){ done = true; } } } } Socket.h: //Socket.h #pragma once #include <iostream> #include "WinSock2.h" using namespace std; class Socket{ protected: WSADATA wsaData; SOCKET mySocket; SOCKET myBackup; SOCKET acceptSocket; sockaddr_in myAddress; public: Socket(); ~Socket(); bool SendData(char*); bool ReceiveData(char*, int); void CloseConnection(); } class ServerSocket : public Socket{ public: void Listen(); void StartHosting(int); } class ClientSocket : public Socket{ public: void ConnectToServer(char*, int); } Socket.cpp: //Socket.cpp #include "Socket.h" Socket::Socket(){ if(WSAStartup(MAKEWORD(2,2), &wsaData)!= NO_ERROR){ cerr << "Error with WSAStartup\n"; WSACleanup(); exit(10); } //Create a socket mySocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); if(mySocket==INVALID_SOCKET){ cerr << "Error creating socket" << endl; WSACleanup; exit(11); } myBackup = mySocket; } Socket::~Socket(){ WSACleanup(); } bool Socket::SendData(char *buffer){ send(mySocket, buffer,strlen(buffer), 0); return true; } bool Socket::ReceiveData(char *buffer, int size){ int i = recv(mySocket, buffer, size, 0); buffer[i] = "\0"; return true; } void Socket::CloseConnection(){ closesocket(mySocket); mySocket = myBackup; } void ServerSocket::Listen(){ acceptSocket = SOCKET_ERROR; while(acceptSocket==SOCKET_ERROR){ acceptSocket = accept(myBackup, NULL, NULL); } mySocket = acceptSocket; } void ServerSocket::StartHosting(int port){ myAddress sin_family = AF_INET; myAddress sin_addr s_addr = inet_addr("0 0 0 0"); myAddress sin_port = htons(port); if(bind(mySocket, (SOCKADDR*), &myAddress, sizeof(myAddress)) == SOCKET_ERROR){ cerr << "Failed to connect\n"; WSACleanup(); exit(14); } if(listen(mySocket, 1) == SOCKET_ERROR){ cerr << "Error listening on socket\n"; } } void ClientSocket::ConnectToServer(char *ipAddress, int port){ myAddress sin_family = AF_INET; myAddress sin_addr s_addr = inet_addr(ipAddress); myAddress sin_port = htons(port); if(connect(mySocket, (SOCKADDR*), &myAddress, sizeof(myAddress)) == SOCKET_ERROR){ cerr << "Failed to connect\n"; WSACleanup(); exit(13); } } It all looks right, and I could swear I followed the video(s) correctly. These are the errors I get: 1>------ Build started: Project: ChatProgram1, Configuration: Debug Win32 ------ 1>Compiling... 1>ChatProgram1.cpp 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2236: unexpected 'class' 'ServerSocket'. Did you forget a ';'? 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2143: syntax error : missing ';' before ':' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2059: syntax error : ':' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2059: syntax error : 'public' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2143: syntax error : missing ';' before '{' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2447: '{' : missing function header (old-style formal list?) 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(4) : error C2143: syntax error : missing ';' before 'using' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(45) : error C2065: 'ServerSocket' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(45) : error C2146: syntax error : missing ';' before identifier 'sockServer' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(45) : error C2065: 'sockServer' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(47) : error C2065: 'sockServer' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(47) : error C2228: left of '.StartHosting' must have class/struct/union 1> type is ''unknown-type'' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(48) : error C2065: 'sockServer' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(48) : error C2228: left of '.Listen' must have class/struct/union 1> type is ''unknown-type'' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(52) : error C2065: 'sockServer' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(52) : error C2228: left of '.ReceiveData' must have class/struct/union 1> type is ''unknown-type'' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(57) : error C2065: 'sockServer' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(57) : error C2228: left of '.SendData' must have class/struct/union 1> type is ''unknown-type'' 1>Socket.cpp 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2236: unexpected 'class' 'ServerSocket'. Did you forget a ';'? 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2143: syntax error : missing ';' before ':' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2059: syntax error : ':' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2059: syntax error : 'public' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2143: syntax error : missing ';' before '{' 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.h(23) : error C2447: '{' : missing function header (old-style formal list?) 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(4) : error C2533: 'Socket::{ctor}' : constructors not allowed a return type 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(16) : warning C4551: function call missing argument list 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(36) : error C2440: '=' : cannot convert from 'const char [2]' to 'char' 1> There is no context in which this conversion is possible 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(45) : error C2653: 'ServerSocket' : is not a class or namespace name 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(46) : error C2065: 'acceptSocket' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(47) : error C2065: 'acceptSocket' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(47) : fatal error C1903: unable to recover from previous error(s); stopping compilation 1>Generating Code... 1>Build log was saved at "file://c:\Users\user\Documents\Visual Studio 2008\Projects\ChatProgram1\ChatProgram1\Debug\BuildLog.htm" 1>ChatProgram1 - 30 error(s), 1 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== I know it's a lot to ask, but please can someone help.. I'm using Visual C++ 2008 express by the way, Thanks In Advance, -Joe

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
unexpected 'class' 'ServerSocket'. Did you forget a ';'?


Quote:
class Socket{

} <--- boo

class ServerSocket : public Socket{


All class and struct declarations must end with ;

Share this post


Link to post
Share on other sites
HiddenHaxor    100
Quote:
Original post by Antheus
Quote:
unexpected 'class' 'ServerSocket'. Did you forget a ';'?


Quote:
class Socket{

} <--- boo

class ServerSocket : public Socket{


All class and struct declarations must end with ;


Oh yeah. That was stupid of me; but it hasn't fixed everything.

I'm stil getting these errors:

1>------ Build started: Project: ChatProgram1, Configuration: Debug Win32 ------
1>Compiling...
1>ChatProgram1.cpp
1>Socket.cpp
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(16) : warning C4551: function call missing argument list
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(36) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(54) : error C2146: syntax error : missing ';' before identifier 'sin_family'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(54) : error C2065: 'sin_family' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(55) : error C2146: syntax error : missing ';' before identifier 'sin_addr'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(55) : error C2065: 'sin_addr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(55) : error C2146: syntax error : missing ';' before identifier 'S_un'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(55) : error C2065: 'S_un' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(55) : error C2228: left of '.S_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(56) : error C2146: syntax error : missing ';' before identifier 'sin_port'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(56) : error C2065: 'sin_port' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(58) : error C2059: syntax error : ','
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(58) : error C2143: syntax error : missing ';' before '{'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(70) : error C2146: syntax error : missing ';' before identifier 'sin_family'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(70) : error C2065: 'sin_family' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(71) : error C2146: syntax error : missing ';' before identifier 'sin_addr'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(71) : error C2065: 'sin_addr' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(71) : error C2146: syntax error : missing ';' before identifier 'S_un'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(71) : error C2065: 'S_un' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(71) : error C2228: left of '.S_addr' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(72) : error C2146: syntax error : missing ';' before identifier 'sin_port'
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(72) : error C2065: 'sin_port' : undeclared identifier
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(74) : error C2059: syntax error : ','
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\socket.cpp(74) : error C2143: syntax error : missing ';' before '{'
1>Generating Code...
1>c:\users\user\documents\visual studio 2008\projects\chatprogram1\chatprogram1\chatprogram1.cpp(20) : warning C4700: uninitialized local variable 'choice' used
1>Build log was saved at "file://c:\Users\user\Documents\Visual Studio 2008\Projects\ChatProgram1\ChatProgram1\Debug\BuildLog.htm"
1>ChatProgram1 - 23 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks In Advance,


Joe

Share this post


Link to post
Share on other sites
rip-off    10976
The errors are generally self descriptive. They include a file and line number, and an error code you can Google to get more information about what the error means.

You appear to have made 0 effort into researching the errors. Remind us why we should expend our energy helping you? When asking a question on a forum, you should be able to include what you have tried

Side point: these are basic language errors. If you can't handle them, this indicates you don't have enough experience with the language to do networking.

Share this post


Link to post
Share on other sites
HiddenHaxor    100
Im really sorrt guys. This morning I woke up and realised that you were right, they were easy errors and when I was tired yesterday I stupidly decided that yould solve them. Looking through the errors it looks like your were correct, and they were very easy errors to fix, in fact I've fixed them all; and I realised I had infact had some experiance with these kind of errors.

However now I encounter a new problem, with linker errors (I have enountered these before, but I'm always rubbish at fixing them), google seems to yield to helpful results. I think it has something to do with library dependencies, but I'm not quite sure what it wants..


These are the errors I get:

1>Socket.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__send@16 referenced in function "public: bool __thiscall Socket::SendData(char *)" (?SendData@Socket@@QAE_NPAD@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function "public: bool __thiscall Socket::ReceiveData(char *,int)" (?ReceiveData@Socket@@QAE_NPADH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: void __thiscall Socket::CloseConnection(void)" (?CloseConnection@Socket@@QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__accept@12 referenced in function "public: void __thiscall ServerSocket::Listen(void)" (?Listen@ServerSocket@@QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "public: void __thiscall ServerSocket::StartHosting(int)" (?StartHosting@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function "public: void __thiscall ServerSocket::StartHosting(int)" (?StartHosting@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "public: void __thiscall ServerSocket::StartHosting(int)" (?StartHosting@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function "public: void __thiscall ServerSocket::StartHosting(int)" (?StartHosting@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "public: void __thiscall ClientSocket::ConnectToServer(char *,int)" (?ConnectToServer@ClientSocket@@QAEXPADH@Z)
1>C:\Users\user\Documents\Visual Studio 2008\Projects\ChatProgram1\Debug\ChatProgram1.exe : fatal error LNK1120: 12 unresolved externals


Thanks In Advance,


Joe

Share this post


Link to post
Share on other sites
HiddenHaxor    100
Never mind, google did have the answer, I was just using the wrong search terms.

I just added this to the header:

#pragma comment(lib, "WS2_32.lib")




Thanks Guys,



Joe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this