TCP 서버 소켓을 열고 적어도 하나의 연결을 허용하고 수신 된 모든 데이터를 에코 백하는 TCP Echo 프로그램을 작성하려고합니다. 최소한이 프로그램은 지정된 포트 (exe : port 4444 ")에서 실행되고 실행되어야합니다. 이상적으로 포트 번호 (명령 줄, 명령 줄 매개 변수 또는 구성 파일에서)를 요청하고 해당 포트에서 열려고 시도하며,TCP Echo 프로그램을 작성 하시겠습니까?
이 프로그램은 Windows 또는 Linux의 telnet 명령 또는 모든 OS의 터미널 에뮬레이터를 사용하여 테스트해야하는데,이 사용은 텔넷이나 하이퍼 터미널을 사용하여 실행중인 프로그램에 연결됩니다. 에 즉시 반향한다 지금까지
을 나는 다음과 같습니다.
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <errno.h>
#define MAXCONNECTING 1000 //Max connection requests
#define BUFFERSIZE 128 //limits data sent per loop
#define ECHOPORT 4444
//Error checking function
//Will be called multiple times to check for errors throughout the code
void ERR(char *ERROR) {
perror(ERROR);
exit(1);
}
/****************************************************************************
* Handles the connection. Receive/Send data from/to client
****************************************************************************/
void ClientHandle(int sock) {
char buffer[BUFFERSIZE];
int received = -1;
//receive the data
if ((received = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
ERR("Failed to receive message from client");
}
//send data and check for more incoming data
while (received > 0) {
if (send(sock, buffer, received, 0) != received) {
ERR("Failed to send data to client");
}
if ((received = recv(sock, buffer, BUFFERSIZE, 0)) < 0) {
ERR("Failed to receive additional data from client");
}
}
close(sock);
}
int main(int argc, char *argv[]) {
/****************************************************************************
* Get port number from command line and set to default port
****************************************************************************/
char *endptr;
short int port;
if (argc == 2) {
port = strtol(argv[1], &endptr, 0);
if (*endptr) {
fprintf(stderr, "EchoServer: Invalid Port Number.\n");
exit(EXIT_FAILURE);
}
} else if (argc < 2) {
port = ECHOPORT; //port 4444
} else {
fprintf(stderr, "EchoServer: Invalid arguments.\n");
exit(EXIT_FAILURE);
}
/****************************************************************************
* Server Configuration. Creating the socket
****************************************************************************/
int serversock, clientsock;
struct sockaddr_in echoserver, echoclient;
if (argc != 2) {
fprintf(stderr, "USING: echoserver Port: 4444\n");
exit(1);
}
//Creating the TCP socket
if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ERR("Failed to create socket");
}
//Constructing the server sockaddr_in structure
memset(&echoserver, 0, sizeof(echoserver)); //clear struct
echoserver.sin_family = AF_INET; //internet ip
echoserver.sin_addr.s_addr = htonl(INADDR_ANY); //listen to any ip address
echoserver.sin_port = htons(atoi(argv[1])); //server port
/*****************************************************************************
* Bind and Listen
*****************************************************************************/
if (bind(serversock, (struct sockaddr *) &echoserver, sizeof (echoserver)) < 0) {
ERR("Failed to bind the server socket");
}
if (listen(serversock, MAXCONNECTING) < 0) {
ERR("Failed to listen on server socket");
}
/*****************************************************************************
* Accepting the transmission
*****************************************************************************/
while (1) {
unsigned int clientleng = sizeof (echoclient);
if ((clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientleng)) < 0) {
ERR("Failed to accept connection from client");
}
fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
ClientHandle(clientsock);
}
return (0);
}
/*****************************************************************************************/
사람이 w를 볼 수 여기 내가 잘못 갔어? 나는 명령을 사용하여 단말기에서 프로그램을 찾을 수 있습니다 : g ++ 다음 EchoServer.cpp -0 EchoServer 다음 : 사용 : ./EchoServer
가 OUPUT은 저를 제공 echoserver 포트 : 나는 시도하고 4444
텔넷으로 연결하는 것이지만, 나는이 물건에 아주 새로운 것이다. 도와주세요!! 당신 것,
g++ EchoServer.cpp -o EchoServer
은 컴파일 후 :
이것을 작성 했으므로 수신 포트를 인수로 지정해야합니다. 즉, './EchoServer 4444'로 시작해야합니다. 어떤 다른 특정 문제가 있습니까? – nos
죄송합니다 무슨 뜻인지 이해가 안됩니다. 나는 어제 소켓에 처음 소개 된이 물건에 아주 익숙하다. 예를 보여줄 수 있습니까? – Zonxwedop
그리고 대답하기 위해, 나는 명령 프롬프트에서 줄을 긋고 Enter 키를 누르고 프로그램이 내가 쓴 것을 정확히 되돌릴 수있는 곳으로 가져 가고 싶습니다. – Zonxwedop