尝试创建TCP客户端

编程入门 行业动态 更新时间:2024-10-22 19:25:20
本文介绍了尝试创建TCP客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我最初是用C#编写的,而我的老板希望将其转换为C ++.我完全不知所措,并且在网上超过2个小时没有发现任何有价值的东西.如果您能将我引向一个可行的例子的方向,那将是不错的选择或帮助我.这是我从C#中获得的代码:

布尔值 bolContinue = false ; // 创建用于TCP连接的TCP客户端 TcpClient tcpClient = 新 TcpClient(); // 将此TCP客户端连接到表单上指定的服务器IP/名称和端口 tcpClient.Connect(strServer,Convert.ToInt32(strPort)); // 创建网络流以检索来自TCP客户端的数据 NetworkStream netStream = tcpClient.GetStream(); // 创建流读取器以读取网络流 System.IO.StreamReader strReader = 新 System.IO.StreamReader(netStream); 如果(tcpClient.Connected) { bolContinue = true ; // 从流阅读器中读取服务器响应行 // MessageBox.Show(strReader.ReadLine()); } 如果(bolContinue == 真) { // 将要写入命令的缓冲区 字节 [] WriteBuffer = 新 字节 [ 1024 ]; ASCIIEncoding enc = 新 System.Text.ASCIIEncoding(); // 将用户名传递到服务器 WriteBuffer = enc.GetBytes(" USER" + strUser + " \ r \ n" ); netStream.Write(WriteBuffer, 0 ,WriteBuffer.Length); // 从流阅读器中读取服务器响应行 // MessageBox.Show(strReader.ReadLine()); // 将密码传递到服务器 WriteBuffer = enc.GetBytes(" PASS" + strPass + " \ r \ n" ); netStream.Write(WriteBuffer, 0 ,WriteBuffer.Length); // 从流阅读器中读取服务器响应行 // MessageBox.Show(strReader.ReadLine()); // MessageBox.Show(即将删除邮件" + intMessage); // 从intMessage中删除消息 WriteBuffer = enc.GetBytes(" DELE" + intMessage + " \ r \ n" ); netStream.Write(WriteBuffer, 0 ,WriteBuffer.Length); // 从流阅读器中读取服务器响应行 // MessageBox.Show(strReader.ReadLine()); // 必须发送退出"以进行删除删除",否则只会标记为删除 WriteBuffer = enc.GetBytes(" QUIT \ r \ n" ); netStream.Write(WriteBuffer, 0 ,WriteBuffer.Length); // 从流阅读器中读取服务器响应 // MessageBox.Show(strReader.ReadLine()); // 从服务器注销 tcpClient.Close(); // MessageBox.Show("Connection Closed","Info"); 返回 真; // 邮件应已删除! } else 返回 false ; // 消息未删除!

这是从删除部分开始的,但是在此示例中,顶部"相同且简单得多.

解决方案

以下是我通常编写TCP客户端的步骤- //创建一个套接字并连接到服务器

WSAStartup<br /> socket<br /> connect<br />

//发送和接收数据

send<br /> recv

//关闭

closesocket<br /> WSACleanup

您可以在MSDN中查找这些函数的语法和参数. .. 就像,在此站点上,有一个用C ++和C#编写的完善的TCP/IP客户/服务器解决方案,这使我得出结论,some1太懒了,无法进行搜索? ah ...尝试在搜索框中输入客户端服务器TCP" ...

简单的Google搜索"sockets C ++"将产生大量示例供您调整.

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#:

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!

解决方案

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.

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 ...

A simple Google search for "sockets C++" will yield plenty of samples for you to adapt.

更多推荐

尝试创建TCP客户端

本文发布于:2023-11-25 09:07:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   TCP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!