创建一个基本的 C/C++ TCP 套接字编写器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16486361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a basic C/C++ TCP socket writer
提问by kmarks2
Below is the following basic socket code I came up with:
下面是我想出的以下基本套接字代码:
//General includes:
#include <iostream>
#include <stdio.h>
#include <string>
//Network related includes:
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
//Target host details:
#define PORT 1234
#define HOST "74.74.74.74"
using namespace std;
//Function prototypes:
string MessageFormat(int, char**);
void MessageSend(string);
int main(int argc, char *argv[])
{
//Parse arguments and format message:
string message = MessageFormat(argc, argv);
//Send the message out:
MessageSend(message);
return 0;
}
string MessageFormat(int argc, char *argv[])
{
//Massage the command line parameters
// into my desired payload format.
return message;
}
void MessageSend(string message)
{
int sd, ret;
struct sockaddr_in server;
struct in_addr ipv4addr;
struct hostent *hp;
sd = socket(AF_INET,SOCK_DGRAM,0);
server.sin_family = AF_INET;
inet_pton(AF_INET, HOST, &ipv4addr);
hp = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
//hp = gethostbyname(HOST);
bcopy(hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
server.sin_port = htons(PORT);
connect(sd, (const sockaddr *)&server, sizeof(server));
send(sd, (char *)message.c_str(), strlen((char *)message.c_str()), 0);
}
This is quite basic, and does in fact work. HOWEVER, it's sending UDP packets instead of TCP packets, so the target host expecting TCP rejects these. Also, by inspecting connect/send values and watching my interfaces with ngrep I can 100% verify the packet is going out, so that's not the issue.
这是非常基本的,并且确实有效。但是,它发送的是 UDP 数据包而不是 TCP 数据包,因此期望 TCP 的目标主机拒绝这些数据包。此外,通过检查连接/发送值并使用 ngrep 观察我的接口,我可以 100% 验证数据包是否已发出,所以这不是问题。
I'm only interested in modifying what I have, not creating a full featured server with boost asio. How can I tweak this so that it operates in terms of TCP instead of UDP?
我只对修改我拥有的内容感兴趣,而不是使用 boost asio 创建功能齐全的服务器。我如何调整它以使其在 TCP 而不是 UDP 方面运行?
回答by Mayank Jain
Following are changes you need to make to transfer data via TCP
以下是通过 TCP 传输数据所需的更改
While creating socket pass correct parameters .In above example you passed SOCK_DGRAM instead pass SOCK_STREAM.
After binding server should go into listen mode (check the manual page of listen) while Client Side should connect after socket creation.
Then accept in server side after listen.
Final Read and write to transfer data
在创建套接字时传递正确的参数。在上面的例子中,你传递了 SOCK_DGRAM 而不是传递 SOCK_STREAM。
绑定后服务器应该进入监听模式(检查监听的手册页),而客户端应该在套接字创建后连接。
然后在服务器端收听后接受。
最终读写传输数据
Diagram attached will give you a clear picture of TCP connection
附上的图表将为您提供 TCP 连接的清晰图片
You can check manual pages for detailed info on all functions or refer beej's guide for socket programming ( use thislink )
您可以查看手册页以获取有关所有功能的详细信息或参考 beej 的套接字编程指南(使用此链接)
回答by Useless
Replace SOCK_DGRAM
with SOCK_STREAM
.
替换SOCK_DGRAM
为SOCK_STREAM
。
Also, read the manualor get a good book.