,其中客戶端發送存儲在緩衝區字符到服務器的Telnet談判我寫了一個套接字程序從客戶端接收的回聲。服務器接收它,並與每個buffer.Here的消息長度響應是代碼:如何從服務器發送查詢和使用Telnet庫
Server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
if(listen(sockfd,5)<0)
{
perror("Error on listen");
}
while(1)
{
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
//fork to hande this client
if (fork() == 0)
{ // client code no longer need this;
close(sockfd);
// loop until a closed or error state happens
ssize_t n =0;
while ((n = read(newsockfd,buffer,sizeof(buffer)-1))>0)
{
printf("Recieved : %*s\n",static_cast<int>(n),buffer);
//send response
static const char resp[] = "I got yout message \n";
n = write(newsockfd, resp , sizeof(resp)-1);
if(n<0)
{
perror("Error writing to socket ");
exit(1);
}
}
close(newsockfd);
exit(0);
}
close(newsockfd); }
return 0;
}
Client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/telnet.h>
char buf1[] = {0xff, 0xfb, 0x18, 0xff, 0xfb, 0x1f};
void read (int sock)
{
char buffer[256];
/* Now read server response */
memset(buffer, 0, sizeof(buffer));
int n = recv(sock, buffer, 255, 0);
if (n < 0)
{
perror("ERROR reading from socket");
return;
}
printf("\n%d bytes received buffer is: %s", n, buffer);
for (int i = 0; i < n; i++)
printf("%2x ", buffer[i]);//printing ascii characters
printf("\n");
}
void mwrite (int sock, char * buf, int size)
{
int n = send(sock, buf, size, 0);
if (n < 0)
{
perror("ERROR writing to socket");
return;
}
printf("Bytes Sent: %d\n", n);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
return(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
return(1);
}
server = gethostbyname(argv[1]);
if (server == NULL)
{fprintf(stderr,"ERROR no such host \n");
exit(0);}
bzero((char *) &serv_addr , sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
return(1);
}
n= write(sockfd,buffer,strlen(buffer));
if(n<0)
printf("ERROR writing in socket %d len %d", n, strlen(buffer));
n = read(sockfd, buffer, 255);
if(n<0)
perror("ERROR reading from socket");
printf("%s\n",buffer);
bzero(buffer,256);
buffer[0] = 0x0d;
buffer[1] = 0x0a;
printf("\nSend buff....");
mwrite (sockfd, buffer,2);
read(sockfd);
mwrite(sockfd, buf1, sizeof(buf1));
printf("\nSend buff1....");
read(sockfd);
printf("\nRecieved all negotiation buffers");
close(sockfd); // close socket
return 0;
輸出是: 客戶端輸出:
debian:~/echoserver$ ./client 127.0.0.1 8088
I got your message
Send buff....Bytes Sent: 2
20 bytes received buffer is: I got yout message
49 20 67 6f 74 20 79 6f 75 74 20 6d 65 73 73 61 67 65 20 a
Send buf1....Bytes Sent: 6
20 bytes received buffer is: I got yout message
49 20 67 6f 74 20 79 6f 75 74 20 6d 65 73 73 61 67 65 20 a
Recieved all negotiation buffers
服務器輸出:
@debian:~/echoserver$ ./server 8088
Recieved : .N=��
Recieved :
=��
我也想知道我們是否想要顛倒這個過程,也就是從服務器發送一個3字節的查詢序列到客戶端,其中byte1-解釋爲命令(0xff),byte2-命令碼,byte3 - 選項代碼。 Telnet流式傳輸,因此收到的消息可能包含一個或多個查詢。例如,服務器在接受連接時發送一個3字節的消息(ff,fd,18)。客戶應該只是附和回來不會(FF,FC,18)
如:提前
Server : Sending Query 1 : 0xff,0xfd,0x18
Client echo : 0xff , 0xfc , 0x18
感謝。
1爲[縮小示例](http://stackoverflow.com/revisions/26182019/3) – HostileFork 2014-10-03 15:45:59
'的static_cast(n)的'嗯,也許添加'C++'標籤。 –
chux
2014-10-08 15:15:55
@chux我想我的服務器發送一個查詢(或多個查詢)到客戶端,該客戶端應以不迴應或不會。如何繼續 – Jonsnow29 2014-10-08 15:20:09