 |
|
 |
Not sure if any of the other comments mention this; the source in the project file download sets the port to listen on 90 and not 80 but the meesages displayed say port 80:
// creates a server socket to listen on port 80
CServerSocket server;
server.SetPort(90);
printf("Server waiting on port 80...\n");
I'm guessing it's a small typo but thought I would mention it anyway.
Thanks for the code though, it works wondefully and is an excellent framwork to build upon. Nice one
|
|
|
|
 |
|
 |
I can't get the sample server to work.
When i compile it there is no error (VSE 2008), have added the ws2_32.lib to the procject.
All files are in the same directory and is in the same project.
When the application is run (i modified it with a try/catch to find the error it says:
Error 10093, Failed to bind: Accept()
What is wrong?
Below is my my modified code.
// headers required by socket classes in sock.h
#include < string>
#include < vector>
using namespace std;
#include < windows.h>
#include "sock.h"
using namespace openutils;
int main()
{
CServerSocket server(100); // server listening on port 100
try
{
CSocket* client = server.Accept(); // accepting a client
char *buffer = new char[100];
client->Read(buffer,99);
// reads some data from client
// client-> sends it back to the client
delete client; // deletes the client socketr
delete[] buffer;
server.Close(); // closes the server
}
catch(CSocketException ex)
{
printf("\nError: %d,%s",ex.GetCode(),ex.GetMessage());
}
}
|
|
|
|
 |
|
 |
Port 100 must be already in use. Please try a port above 1024.
|
|
|
|
 |
|
 |
Still geting the same error. Tried various ports over 1024.
|
|
|
|
 |
|
|
 |
|
 |
int CSocket::Read(char* buffer,int len) throw (CSocketException) {
int nret = 0;
nret = recv(m_socket,buffer,len,0);
buffer[nret-1] = '\0';
return nret;
}
Why would we want to replace the last read byte with '\0' ?
|
|
|
|
 |
|
 |
Thanks for pointing out this bug!
A fix has being submitted.
|
|
|
|
 |
|
 |
The program will always listen to the connection, but also can send message to another computer ( that running the same program ). And the program will do something when receive a specific message .
Thanks in advance
----------
C++ Learner
|
|
|
|
 |
|
 |
You need to run two threads, one for the socket server (listener) and the other for the socket client (to send requests). It seems you are trying to implement
a peer-to-peer system. You might want to look at the implementation of
BitTorrent or something similar for ideas.
|
|
|
|
 |
|
 |
I want to send an unsigned char* message.
It seems sock.cpp will call "send" function in winsock.h but it only accepted char*.
|
|
|
|
 |
|
 |
You can send unsigned char* after casting it into char*.
On the receiving side you should unmarshel it back to unsigned char*.
|
|
|
|
 |
|
 |
I have a small app that I modified to use these classes for input and output rather than stdin/stdout. I would like to publish that code, but I need to know what your license is. Can I publish the program including your source code?
The application that would be using it is copyright another company. Also, the application would be published in source code form.
|
|
|
|
 |
|
 |
You can use the code anyway you like.
Just give me credit for the library, thats all!
|
|
|
|
 |
|
 |
Hello...
When I try to build the socket class library I get compiler errors. I'm using Dev-C++ and get these errors:
C:\test-apps\cpp\sockets>c:\Dev-cpp\bin\c++ sock.cpp
In file included from sock.cpp:20:
sock.h:62: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
sock.h:88: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
sock.h:89: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
sock.h:91: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
sock.h:92: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
sock.h:115: error: invalid use of undefined type `struct openutils::CSocketException'
sock.h:28: error: forward declaration of `struct openutils::CSocketException'
I'm not sure why it does not like that type, as it's there in the header. I do have exception handling turned on in the compiler, although I think that is unrelated.
Any suggestions?
Thanks...
|
|
|
|
 |
|
 |
Is sock.cpp included in your project?
Try to move the declaration of the CSocketException class to the
begining of sock.h
|
|
|
|
 |
|
 |
I moved the class to the top of the header file and that fixed it, thanks very much. I'm using Dev-C++ and it's a very picky compiler! Nice environment though.
Now I can play with these classes. They look very nice. I'll post an update in a few days.
|
|
|
|
 |
|
 |
Okay, that worked and I have the code in use. This is a nice, basic socket class, thank you very much!
I had a need to write binary data to the socket. Using the send method fails because it uses strlen to caculate the length of the data to be sent, so I added this method:
int CSocket::Send(const char* data, int len) throw (CSocketException) {
int nret = send(m_socket,data,len,0);
if(nret == SOCKET_ERROR) {
nret = WSAGetLastError();
throw CSocketException(nret, "Network failure: Send()");
}
return nret;
}
Do you have any plans for any enhancements to this class? Adding a flush method would be handy. I took a quick look at winsock (which I know very little about) but didn't see a winsock flush, so I'm not sure if its doable or not.
Thanks again!
|
|
|
|
 |
|
 |
Fine, thank you.
I don't have plans to update the code any soon.
|
|
|
|
 |
|
 |
Hello!
I've the following problem and need help...
This is a code snippet from my Java server loop:
...
ServerSocket serverSocket = null;
Socket newConnection = null;
try {
serverSocket = new ServerSocket(12345);
while (true) {
newConnection = serverSocket.accept();
Thread myThread = new ServerThread(newConnection);
// ... where the ServerThread class extends the Thread class
myThread.start();
}
} catch (Exception e) {
System.exit(1);
}
...
Now I've to implement this server loop in C++.
Dealing with the WinSock API functions is not easy,
but fortunally I found your great Thread and Socket classes here.
But in C++ is the problem to keep all references of the started threads,
because they must terminated and deleted programmatically.
How can I solve this?
Do you have a short example of a multithreaded server implemented with your classes?
That would be great. Please help me...
Thanks in advance.
Steffen
|
|
|
|
 |
|
 |
With my classes, you don't need to do special memory management. All resources are released when the Thread and Socket objects go out of scope. If you are using pointers to these objects u can store them in a vector, and later loop
thru the vector and delete each entry. The code will look something like this:
// global vector
std::vector<Socket*> sockets;
// ......
// local initialization
Socket* s = new Socket();
sockets.push_back(s);
// ......
// program termination
std::iteratot iter = std::vector<Socket*>::iterator;
for(iter = sockets.begin(); iter != sockets.end(); iter++)
delete *iter;
// ....
I think this has sufficiently answered your question??
|
|
|
|
 |
|
 |
Thanks a lot for your thought-provoking impulse...
Now I've an idea how to port my code.
|
|
|
|
 |
|
 |
I need help please…
I need to develop a sample a client server program doing summation and need to to satisfy the following requirement.
Server (summation-server.c)
1.Computes the sum of numbers inputted by the user through the client program (these numbers may either be integers or floating-point numbers);
2.During invocation from the command line, requires a port number where it will be listening for connection requests, and messages from the client;
3.Continuously runs in the background, listening for inputs from the client program until terminated by pressing Ctrl-C;
4.Understands the following messages received from the client:
o inputs ..........where N is the number of numbers to do a
summation on.
o num .................where n is one of the N numbers to sum
o sum .........................the sum of the N numbers
5.Does nothing if it does not understand the message;
6.Does input validation to reject invalid inputs;
7.Resets the environment variables after the sum was outputted on the screen.
8.State sensitive, meaning it cannot backtrack to any previous process. Process 4 needs to be satisfied before the server’s state cycles back to process 1.
For the Client (summation-client.c)
1.Description: Used to provide inputs to the server program;
2.During invocation from the command line, requires the machine name where the server is running, and the port number where the server is listening;
3.Automatically connects to the summation-server;
4.Continuously runs in the background, providing a means by which the user can send inputs to the summation server until terminated by pressing Ctrl-C;
5·Provides a command line prompt where the user can send text messages to the server;
6.Messages entered at the command line gets sent to the summation-server upon pressing the Enter key;
7·The valid messages that may be sent to the summation-server are those mentioned above, namely: inputs, num, and sum.
8·Does not do data entry validation; Meaning, the user is free to send any message to the summation-server.
This is the sample screen from what i understand based on the given requirements.
Sample server screens Sample client screens
prompt$ ./summation-server 31219 prompt$ ./summation-client latte 31219
Server is running... latte>_
prompt$ ./summation-client latte 31219
latte>inputs 2
latte>_
prompt$ ./summation-server 31219
Server is running...
Waiting for 2 numbers...
prompt$ ./summation-client latte 31219
latte>inputs 2
latte>num 100
latte>_
prompt$ ./summation-server 31219
Server is running...
Waiting for 2 numbers...
num1 is 100...
prompt$ ./summation-client latte 31219
latte>inputs 2
latte>num 100
latte>num 150
latte>_
prompt$ ./summation-server 31219
Server is running...
Waiting for 2 numbers...
num1 is 100...
num2 is 150.
prompt$ ./summation-client latte 31219
latte>inputs 2
latte>num 100
latte>num 150
latte>sum
latte>_
prompt$ ./summation-server 31219
Server is running...
Waiting for 2 numbers...
num1 is 100...
num2 is 150.
sum is 250.
Do you think it is correct with regards to my understanding how the program process?
But the requirements to solve this problem is use a C/C++ program. Please help with this…
Thanks in advance.
emmie
|
|
|
|
 |
|
 |
Pls go thru my article and sample code, and you will find it's really easy to solve ur problem using c++ (if u have some basic understanding of the language)...
|
|
|
|
 |
|
 |
Dear Vijay Mathew Pandyalakal, For me, your code is as pratical as Java is : can't use it as it is. As 101% of us, I'm using a proxy !... How can I set my sockets to open using my "default Internet settings" (encapsulating the proxy options or not, etc..) please?
|
|
|
|
 |
|
 |
hey guy, just think with your head...
This is a GENERIC library, "all" things thats he does is connect, send and reciev data.
A proxy is just a server thats redirect your connections as you ask, but if you dont ask it will do nothing...
If you wanna send a postcard to your brother, did you adress it to your mail office?
You must connect to the proxy, ask it for redirect en then send your packet.
read the RFC for HTTP or Socks proxy, depending on your's.
-> www.google.com
P.S. sorry english is not my language.
|
|
|
|
 |