Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I originally wrote this in C# and my boss would like this converted to C++. I'm at a total loss and over 2 hours on the web has turned up nothing of value. If you could just point me in the direction of a working example that would be great or give me a hand. This is the code I have from C#:
C#
Boolean bolContinue = false;
            
            // create a TCP Client for a TCP Connection
            TcpClient tcpClient = new TcpClient();
            
            // connect this TCP Client to the server IP/name and port specified on the form
            tcpClient.Connect(strServer, Convert.ToInt32(strPort));
            // create a network stream to retreive data from the TCP Client
            NetworkStream netStream = tcpClient.GetStream();
            // create a stream reader to read the network stream
            System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);
            if (tcpClient.Connected)
            {
                bolContinue = true;
                // read the server response line from the streamreader
                //MessageBox.Show(strReader.ReadLine());
            }
            if (bolContinue == true)
            {
                // Buffer to which the commands will be written
                byte[] WriteBuffer = new byte[1024];
                ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                // pass user name to the server
                WriteBuffer = enc.GetBytes("USER " + strUser + "\r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                // read the server response line from the streamreader
                //MessageBox.Show(strReader.ReadLine());
                // pass the password to the server
                WriteBuffer = enc.GetBytes("PASS " + strPass + "\r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                // read the server response line form the streamreader
                //MessageBox.Show(strReader.ReadLine());
                //MessageBox.Show("About to delete message " + intMessage);
                //Delete the message from intMessage
                WriteBuffer = enc.GetBytes("DELE " + intMessage + "\r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                // read the server response line form the streamreader
                //MessageBox.Show(strReader.ReadLine());
                //Must send the Quit to make the delete "take", otherwise it's just marked for deletion
                WriteBuffer = enc.GetBytes("QUIT\r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                // read the server response from the streamreader
                //MessageBox.Show(strReader.ReadLine());
                // Log off from the server
                tcpClient.Close();
                //MessageBox.Show("Connection Closed","Info");
                return true; // Message should have been deleted!
            }
            else return false; //Message not deleted!

This is from the delete portion, however the "top" is the same and a lot simpler in this example. Thanks in advance!
Posted
Comments
Manfred Rudolf Bihy 20-Oct-11 14:43pm    
Are we talking CLI/C++ or is it MFC C++?
MacRaider4 20-Oct-11 15:44pm    
Actually at this point I'm talking just regular plain old C++... I have something working but I'm getting some funky results. I'll have some code to post tomorrow with some output. I've gotten a connection at least. Using winsock2 and ws2tcpip. Well at least they are in the example.

These are the steps I normally do to write a TCP client -

//Create a socket and connect to the server
WSAStartup<br />
socket<br />
connect<br />

// To send and receive data
send<br />
recv


// Shutting down
closesocket<br />
WSACleanup


You can look up the syntax and parameters to these functions in MSDN.
 
Share this answer
 
Comments
MacRaider4 21-Oct-11 7:35am    
That would be what I eventually came up with... biggest problem is I was searching on C++ POP3, C++ connect to mail server and the like... Just stuff that comes with exp I guess. Thanks!
Well... The point is ,, You both have the point , yet , there is small "issue" here ...

Like, there is a perfect client/server over TCP solution written in C++ AND C# right here on this site , which leads me to conclusion that some1 is too lazy to SEARCH ???

Bah ... try typing in "Client server TCP" in search box up there ...
 
Share this answer
 
Comments
MacRaider4 21-Oct-11 7:21am    
Actually I spent over 2 hours searching and found a bunch of examples that were really fragmented and a lot left out some of the important parts, like use winsock2 or winsock. Believe it or not some of us don't know about all the libraries :-) However thank you for the additional help in what to search for
A simple Google search for "sockets C++" will yield plenty of samples for you to adapt.
 
Share this answer
 
Please forgive me for the off-topic post. Maybe you will feel I became a complete misanthrope, but… maybe my advice could somehow help you. You see, I feel quite irritated by your question because you write it like "I originally wrote this in C# and my boss would like this converted to C++". I don't want to say it's rude, but it looks not quite correct as well.

I'm afraid to be rude myself, but my first feeling was to answer: "than let your boss write to us and ask the same question after proper explanations why such stupid thing happened". You see, if you think that you should obey your "boss" who do you think are we? The problem is not only technical and not mostly technical…

When you ask a question, you would like to talk to a mature person who really takes responsibility for the solution and has a will to solve the problem, not a middleman who refers to somebody else.

We all only volunteer to answer questions if we feel it interesting. One of the important factors is feeling that our work really helps someone, and one important condition of it is that the inquirer would have a true desire to solve the problem. If this is the situation forced by a "boss" or someone, not real interest to the work itself, it becomes really boring. You're really trying to involve other people in your problems of your own work communications. After all, this is just your job? Why did you guys allowed some miscommunications which lead to a piece of job written in a wrong language? How come the job is assigned to a person not qualified to do it, and why this person agree to do it and possibly get in trouble? There is nothing really which need expertize of people volunteering for CodeProject. Translation of the work from C# to C++? This is just your job? Knowledge of C++ syntax or related libraries? Well, just learn it. This is not a matter of CodeProject "Quick Answers". Sorry, but this is what it is.

—SA
 
Share this answer
 
v2
Comments
MacRaider4 20-Oct-11 18:50pm    
To show who the mature person is here, I'm not going to comment. I'll just say re-read what I actually asked.
Sergey Alexandrovich Kryukov 20-Oct-11 21:03pm    
I am sorry. Not that I have anything personal. Nevertheless, I think what I think.
--SA
MacRaider4 21-Oct-11 7:17am    
That's fine, but what you said was unfounded. I'll be the first to admit that my C++ exp is limited to school and about 12 months of just reworking/fixing existing bugs. So when it comes to starting from scratch on a project I'm a bit new at it. Coming from a VB and C# background of the last 9 years it's a bit rough getting started at times as there are a lot of things you need to know about that you didn't have to before (again still learning those things). That is why I said point me in the direction of a working example... that way I'd be able to figure out the rest. I completely forgot about the socket portion and that is what was throwing me off.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900