简体   繁体   English

C ++在Linux中进行套接字编程

[英]Socket programming in Linux by C++

I am developing a C++ app in openSUSE 12.3 and one of it's part is responsible to send data to a device via Socket (in LAN). 我正在openSUSE 12.3中开发一个C ++应用程序,其中一个部分负责通过Socket(在LAN中)向设备发送数据。 I am using this code 我正在使用此代码

int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *printer;    
portno = 9100;  

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) error("ERROR opening socket\n"); 
printer = gethostbyname("100.0.69.23");
if(printer == NULL) error("No such device on 100.0.69.23\n");
//set bit set to zero
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) printer->h_addr, (char *) &serv_addr.sin_addr.s_addr, printer-     >h_length);

serv_addr.sin_port = htons(portno); 

if(connect(sockfd, (struct sockaddr *) & serv_addr, sizeof(serv_addr)) < 0) 
    {error("ERROR connecting");
           return;
     }

n = write(sockfd, data, datalenght);
if(n < 0) error("ERROR sending command to printer");
n = read(sockfd, buffer, 200);

I think the code is correct but the connect function returns -1 and seems that could not connect to the device (printer) . 我认为代码是正确的但是connect函数返回-1并且似乎无法连接到设备(打印机)。 This code was written in openSUSE 11 and was working OK and I could send/receive data to device but when I copy/paste it to new system (openSUSE 12.3) it gives me failure in connecting. 这段代码是用openSUSE 11编写的,工作正常,我可以向设备发送/接收数据但是当我将它复制/粘贴到新系统(openSUSE 12.3)时,它会让我无法连接。 I ping result on the specific IP which is in use show that device is reachable via LAN 我在正在使用的特定IP上ping结果显示设备可通过LAN访问

I think you should consider the possibility that hostent returned by gethostbyname function might have AF_INET6 address family (in which case it will be IPv6 instead of IPv4 address). 我想你应该考虑的可能性hostent通过返回gethostbyname功能可能有AF_INET6地址族(在这种情况下,将支持IPv6,而不是IPv4地址)。

http://linux.die.net/man/3/gethostbyname http://linux.die.net/man/3/gethostbyname

So you can either use GNU extension function gethostbyname2 function that will allow you to specify address family. 因此,您可以使用GNU扩展函数gethostbyname2函数来指定地址族。

printer = gethostbyname2("100.0.69.23", AF_INET);

Or instead you can use getaddrinfo function, as gethostbyname function is said to be obsolete, by the documentation. 或者你可以使用getaddrinfo函数,因为文档说gethostbyname函数已经过时了。

As already mentioned, you are checking for printer == NULL before initializing it. 如前所述,您在初始化之前检查printer == NULL I think you meant the following instead: 我认为你的意思是:

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket\n");
printer = gethostbyname("100.0.69.23");
...

Also the structure of the code seems to indicate that when you want to send a command to the printer you connect() , write() then read() , which is OK if you are only ever sending one command, but suboptimal if you are sending multiple commands. 此外,代码的结构似乎表明当你想向打印机发送命令时你connect()write()然后read() ,如果你只发送一个命令就没关系,但如果你是发送多个命令。 In the latter case you want to separate the connect() from the write() as it's fairly expensive to connect so you want to do it just once. 在后一种情况下,您希望将connect()write()分开,因为connect()相当昂贵,因此您只需要执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM