How to restart a sockets program? - c++

I need my server to stay connected to the server. Does anyone know how to do this? Or post links tutorials anything?
Also it says when it restarts 'could not accept client' so how would I clear everything and make it accept it?

Server code:
For your server side code, do a loop wrapping the accept call. For the accepted socket that is created create a new thread, so that the next accept will be called right away.
On server startup you may also want to use the SO_REUSEADDR flag. That way if you had a crash, or even a fast restart of the program, then your server will be able to use the same port again without a problem.
Client code:
For your client code you would just check for a socket error and if that occurs just establish a new connection.
Other resources:
Beej's guide to network programming is a great resource for learning socket programming.
Frostbytes.com also has a great tutorial on socket programming.
If you want something more in depth, check out Unix Network Programming 3rd Edition by W. Richard Stevens.
Other options:
Instead of plain bsd-style sockets, you could also try using boost asio for easier socket programming. You could check out their examples page.

Related

c++ winsock tcp server and client connection with the internet

I have a question and I hope someone can help me.
I have a simple TCP chat programmed with Winsock. Unfortunately, it still works only local at the moment. I would like to have it on the Internet, so I can chat with friends just for fun.
Now I have finally come to the point where I have to unlock ports on my router, and I want to avoid that because then my PC plays the server and as soon as it's off, the chat is offline again. I would run it generally in a virtual server, but I'm not sure how, and where I can get the best server for this.
Do I need a Windows server volume, or are there other cool possibilities?
It would be really nice if someone can give me suggestions on how I can implement this best.

C++ & Boost: I'm trying to find an example TCP program with a server that accepts connections from multiple clients

A chat program would be a good enough example.
Just need a server that can accept multiple connections from the clients, and the server needs to be able to send messages to individual clients.
I plan to turn this into a distributed computing program to work with multiple Neural Networks.
Asio is the Boost library that handles networking. There's a chat server example listed here.
I cannot give you an example progam. But to write a server things that you have to do:
1. server will listen at a port for connection
2. thread pool which will accept the connection and serve request
3. write the server code in thread safe manner
You have to use socket programming A good link for that http://beej.us/guide/bgnet/
you can use win32 api in windows and posix for linux

C++ TCP server Class Design

I am developing TCP server in C++(win32/linux) which cater multiple client.The server is for Video Streaming.Client request video to server and Server get it from Gateway connected with camera.
I am stuck up in the class Design.I found three classes by
Peer
Session and
ConnectionMgr.
So here ConnectionMgr is responsible for managing other Classes.
I wanted your feedback on this.
what info Peer and session need to have;
How Peer and session is related
what information needs to be modeled here.
how to do Session maintainer.
Managing multiple client will require Threads what information thoses may need.
Please give your feedback so that I can upgrade my design.
Looking at the problem space from scratch:
there's some state associated with each client that connects - you seem to split this between Peer and Session and I see no real value in that if they're 1:1 - can omit such trivia from the high-level design stage.
"what info Peer and session need to have": socket descriptor is the only crucial thing, assuming you have only one camera and stream to all clients at the same pace (losing data when socket send() blocks/can't complete due to full buffer), otherwise, a buffer too...
you have a ConnectionMgr, well - yes... it must listen and accept clients on the server socket, possibly launch a new thread per client or monitor the set of current client connections and dispatch events
you'll need to make some decisions about the I/O and concurrency model (e.g. select/poll/non-blocking, async, blocking, single threaded, thread-per-client, thread-pool etc)
this will obviously affect your design: you should decide which - or which choices - you need to support...
To get a feel for this problem space, I suggest you create a very simple client/server program - probably using threads if you're familiar and comfortable with multithreading, otherwise you can hack upon the GCC libc TCP client/server examples for a select() based solution (http://www.gnu.org/s/libc/manual/html_node/Server-Example.html#Server-Example) or try boost::asio or ACE or whatever. To start, just get it working so you can telnet to the server and whatever you type in any connection is echoed out on all the connections. That should give you enough insight to start asking more concrete questions.
As #nabulke and #Jan Hudec stated in their comments, Boost.Asio is very good solution for your problem. Have a look at pretty simple example "Async TCP Echo Server". It uses just 2 classes: server and session. No session_manager. Sessions are managed automatically by smart pointers, very convenient and simple approach.
Using Boost.Asio you can keep the network part simple (and almost optimal by efficiency using asynchronous processing). As a bonus, adding couple of code lines, you receive multithreaded server w/o headache (I would recommend this example: "An HTTP server using a single io_service and a thread pool calling io_service::run().", just ignore HTTP stuff. pay attention to boost::asio::io_service::strand used in connection class)

Online multiplayer Qbasic Gorillas?

I want to play that Qbasic Gorillas game with someone that lives in Florida.
Here's the flash version online
Gorillas
This link is to someone's post about the remake he programmed - there is a link to the game above in there and also his source code for it
playerio.com
If possible, how can I modify the code so that I can play against them over the innernet?
Should the game be public, private, or some combination of both?
It would also be cool if you could replay tosses or entire rounds with their respective angle/velocity inputs.
Take a look at sockets. Sockets are how you can connect two computers over the internet. In most implementations of sockets, you have a server socket and a client socket. The server socket listens for connections, and the client socket tries to connect to a server socket. In your case with just you and your friend, it doesn't matter much which of you is the server or client, but you will have to program for both. You also have to choose a protocol to use. The two protocols for online gaming are TCP and UDP. TCP is the most common and it is a reliable "guaranteed" connection (TCP would send data that is important). UDP is a connectionless protocol where the client just sends data with no guarantee that the data will actually get there. UDP is mostly used for very frequent updates in online games (UDP would most likely be used to send positional data in a first person shooter for example). So with your protocol in mind, I would start by adding a simple chat feature to the game. That way, you can see something working and start to understand what is happening better.
Just to suggest an alternative approach, if you want to play a game that is local multiplayer, you could set up DosBox or a Virtual Machine and then install a VNC server that both of you can log into. This would give you both KVM control.
Since it is a take-your-turn game. you could even use a chat application that has the ability to share keyboard and mouse input.

Socket writing problem with TCP/IP

I am using TCP/IP socket example i.e. "echoclient", and I am facing problems with writing and reading.
I am connecting to server socket but it shows null data. I don't know whether it is data conversion problem or any other issue.
Download Wireshark and see if it helps debug. It can peek all network traffic.
In case if you are connecting via mobile, check with your GPRS/EDGE/3G service provider if they are not restricting arbitrary IPs ?
Also in case if you're connecting from Simulator on PC, check if you have Proxies over your network before internet ?
It is always a good idea to use well tested libraries and implementation. Socket programming is c++ requires good understanding of OS if you want to write potable code.
Try Ace Wrappers - http://www.cs.wustl.edu/~schmidt/ACE.html It provide well tested pattern based C++ implementation for distributed network programming.