我只想编写一个应用程序,它连接到我的SSH服务器,发送消息并接收响应。我不想登录,或者类似的东西,只是发送消息并获得响应。我有一个openSSH
服务器在端口192.168.0.1
上监听22
。
然而,我的代码(当然发送并获取消息),打印:
SSH-1.99-OpenSSH_4.4
Protocol mismatch.
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int sendAll(int socket, char *buffer, int numBytes)
{
int ret; // Return value for 'send'
int sentBytes; // Total number of bytes sent
// Send the given number of bytes.
sentBytes = 0;
while (sentBytes < numBytes)
{
ret = send(socket, buffer + sentBytes, numBytes - sentBytes, 0);
// Error encountered.
if (ret == -1)
{
close(socket);
perror("send");
exit(1);
}
// Data sent.
else
{
sentBytes += ret;
}
}
return sentBytes;
}
int recvAll(int socket, char *buffer, int numBytes)
{
int ret; // Return value for 'recv'
int receivedBytes; // Total number of bytes received
// Retrieve the given number of bytes.
receivedBytes = 0;
while ((receivedBytes < numBytes) && ret != -1)
{
ret = recv(socket, buffer + receivedBytes, numBytes - receivedBytes, 0);
// Error encountered.
if (ret == -1)
{
close(socket);
perror("recv");
exit(1);
}
else if(ret == 0)
{
receivedBytes += ret;
break;
}
// Data received.
else
{
receivedBytes += ret;
}
}
buffer[receivedBytes] = '\0';
return receivedBytes;
}
int main(int argc, char **argv)
{
char * ip_addr = "192.168.0.1";
int port = 22;
int sockfd;
struct sockaddr_in server_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_aton(ip_addr, &server_addr.sin_addr);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect");
close(sockfd);
exit(1);
}
char msg[] = "abc\n";
char buffer[255];
int recv_size, send_size;
send_size = sendAll(sockfd, msg, strlen(msg));
recv_size = recvAll(sockfd, buffer, 254);
buffer[recv_size] = '\0';
printf("%s\n", buffer);
return 0;
}
另一方面,当我用recvall
替换recv
时,它只会显示:SSH-1.99-OpenSSH_4.4
。但为什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int sendAll(int socket, char *buffer, int numBytes)
{
int ret; // Return value for 'send'
int sentBytes; // Total number of bytes sent
// Send the given number of bytes.
sentBytes = 0;
while (sentBytes < numBytes)
{
ret = send(socket, buffer + sentBytes, numBytes - sentBytes, 0);
// Error encountered.
if (ret == -1)
{
close(socket);
perror("send");
exit(1);
}
// Data sent.
else
{
sentBytes += ret;
}
}
return sentBytes;
}
int recvAll(int socket, char *buffer, int numBytes)
{
int ret; // Return value for 'recv'
int receivedBytes; // Total number of bytes received
// Retrieve the given number of bytes.
receivedBytes = 0;
while ((receivedBytes < numBytes) && ret != -1)
{
ret = recv(socket, buffer + receivedBytes, numBytes - receivedBytes, 0);
// Error encountered.
if (ret == -1)
{
close(socket);
perror("recv");
exit(1);
}
else if(ret == 0)
{
receivedBytes += ret;
break;
}
// Data received.
else
{
receivedBytes += ret;
}
}
buffer[receivedBytes] = '\0';
return receivedBytes;
}
int main(int argc, char **argv)
{
char * ip_addr = "192.168.0.1";
int port = 22;
int sockfd;
struct sockaddr_in server_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_aton(ip_addr, &server_addr.sin_addr);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect");
close(sockfd);
exit(1);
}
char msg[] = "abc\n";
char buffer[255];
int recv_size, send_size;
send_size = sendAll(sockfd, msg, strlen(msg));
if ((recv_size = recv(sockfd, buffer, 254, 0)) == 0)
{
close(sockfd);
if (errno != 0)
{
perror("recv");
exit(1);
}
}
buffer[recv_size] = '\0';
printf("%s\n", buffer);
return 0;
}