Data Packets

Started by
21 comments, last by hplus0603 14 years, 11 months ago
Well the way I read is that you need to have a thread to accept more then one connection at a time...
Advertisement
Quote:Original post by ARC inc
Well the way I read is that you need to have a thread to accept more then one connection at a time...


Luckily you don't. If that was the case, programming even the simplest server would be a nightmare. =)

Quote:I am wanting to make threads in my Server Architect so I can accept more then just a single connection.


Please read the Forum FAQ. There is no direct correlation between threads and clients in general. For someone starting out with network programming, using select() is fine, and can do 64 connections on Windows, and about a thousand on UNIX. Once you have that working fine, and actually have more than 60 simultaneous users, you can start looking into more advanced systems.

Also, if you're using UDP, you only need a single socket no matter how many "connections" you have.
enum Bool { True, False, FileNotFound };
Ok, so I have my basic server done but I am getting errors with it....can someone help me please?

#include <winsock2.h>#include "config.hpp"#include "util.hpp"#include <stdio.h>class server{    private:WSADATA wsaData;int ErgoServ = WSAStartup(MAKEWORD(2,2), &wsaData);if(ErgoServ != 0){    printf("WSAStartup Failed: %s\n", WSAGetLastError);    WSACleanup();    return 1;}SOCKET s = socket(AF_INET, SOCK_STREAM, 0);if(s == INVALID_SOCKET){    printf("Socket creation failed: %s\n", WSAGetLastError);    WSACleanup();    return 1;}sockaddr_in sin;sin.sin_port = htons(80);sin.sin_addr.s_addr = INADDR_ANY;sin.sin_family = AF_INET;if(bind(s(sockaddr*)&sin sizeof(sin)) == SOCKET_ERROR){    printf("Bind failed: %s\n", WSAGetLastError);    WSACleanup();    return 1;}while(listen(s, SOMAXCONN) == SOCKET_ERROR);SOCKET client;int lin = sizeof(sin);client = accept(s,(sockaddr*)&sin, &lin);printf("Connection Establised");char buf[80] = PACKET_WELCOME;send(client,buf,sizeof(buf),0);closesocket(s);closesocket(client);WSACleanup();return 0;}


||=== Test, Debug ===|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: `int WSAStartup(WORD, WSADATA*)' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: `server::wsaData' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: `&' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: a function call cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: ISO C++ forbids initialization of member `ErgoServ'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: making `ErgoServ' static|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|error: ISO C++ forbids in-class initialization of non-const static member `ErgoServ'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|14|error: expected unqualified-id before "if"|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|error: `SOCKET socket(int, int, int)' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|error: a function call cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|error: ISO C++ forbids initialization of member `s'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|error: making `s' static|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|error: ISO C++ forbids in-class initialization of non-const static member `s'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|22|error: expected unqualified-id before "if"|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|29|error: ISO C++ forbids declaration of `sin' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|29|error: expected `;' before '.' token|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|30|error: ISO C++ forbids declaration of `sin' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|30|error: expected `;' before '.' token|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|31|error: ISO C++ forbids declaration of `sin' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|31|error: expected `;' before '.' token|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|33|error: expected unqualified-id before "if"|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|39|error: expected unqualified-id before "while"|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|28|error: invalid use of non-static data member `server::sin'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|43|error: from this location|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|43|error: ISO C++ forbids initialization of member `lin'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|43|error: making `lin' static|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|43|error: ISO C++ forbids in-class initialization of non-const static member `lin'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `SOCKET accept(SOCKET, sockaddr*, int*)' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `server::s' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `server::sin' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `&' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: a casts to a type other than an integral or enumeration type cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `server::lin' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `&' cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: a function call cannot appear in a constant-expression|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: ISO C++ forbids declaration of `client' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: ISO C++ forbids initialization of member `client'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: making `client' static|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: ISO C++ forbids in-class initialization of non-const static member `client'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: declaration of `int server::client'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|41|error: conflicts with previous declaration `SOCKET server::client'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|45|error: expected identifier before string constant|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|45|error: expected `,' or `...' before string constant|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|45|error: ISO C++ forbids declaration of `printf' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|45|error: ISO C++ forbids declaration of `parameter' with no type|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|47|error: `PACKET_WELCOME' was not declared in this scope|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|47|error: ISO C++ forbids initialization of member `buf'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|47|error: making `buf' static|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|47|error: invalid in-class initialization of static data member of non-integral type `char[80]'|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|48|error: `client' is not a type|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|

Thouse are the errors..I haven't read through them all most of them I can fix myself but some of the networking errors I am having problems with...

Thanks to all that have been helping me through this post :D

Inside a class, you may only put declarations *. You need to move the code to a function body.

Think about the data the server needs to keep between functions. This is its state. Your server will probably want to store the listen socket, as well as the sockets of each of its clients. These are its private member variables. The other values would be local variables inside various functions.

* In general. You can define small functions and initialise static const integral values.
Ok then
ok, so I rewrote my server function stuff, so it uses

int serverstart(){

but now I am getting errors with '*' it says like expected error with primary function '*'

Why tell us what the errors are like, when you can just copy and paste them here. Posting your function would help too.
||=== Test, Debug ===|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h||In function `int serverstart()':|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|12|warning: char format, different type arg (arg 2)|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|20|warning: char format, different type arg (arg 2)|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|30|error: expected primary-expression before '*' token|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|30|error: expected primary-expression before ')' token|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|30|error: `s' cannot be used as a function|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|31|warning: char format, different type arg (arg 2)|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|error: `PACKET_WELCOME' was not declared in this scope|
C:\Documents and Settings\Dev\Desktop\Ergo\Test\socket.h|44|warning: unused variable 'PACKET_WELCOME'|
||=== Build finished: 4 errors, 4 warnings ===|

an it's all the same code cept I am not using the class server

I am using

int serverstart(){//All my Server Code: WSADATA etc etc...}


-Edit- I know the PACKET_WELCOME problem ignore that error
The error message "char format, different type arg (arg 2" is because you are using printf incorrectly. Your printf messages should be like this:
printf("<operation> failed: %s\n", WSAGetLastError());


Some of your other errors might be related to lines like this:
bind(s(sockaddr*)&sin sizeof(sin))

You probably want a commas between your arguments "s", "(sockaddr*)&sin" and "sizeof(sin)".

For future reference, please copy and paste the exact code. It actually requires less effort than typing up a skeleton, and shows us very clearly what is going on. I had to scroll up and down trying to match the error messages in your later post with the code from your earlier one.

A final comment. The kind of error messages you are getting have nothing to do with sockets or network programming. They are basic C++ language problems. If you cannot solve them, that indicates to me that you are going too fast.

Trying to do network programming when you are still a beginner at C++ will probably only cause you a lot of pain and frustration. I would advise that you stick to simpler programs, and move on when you feel you are ready.

This topic is closed to new replies.

Advertisement