Telnet client server session with negotiation - c++

I am trying to write a code for Telnet client server session which sends negotiations between them. Like WILL, WON'T,DO , DON'T. I wrote a basic client-server program using socket programming.
It would be of great help if I could know how to modify client/server as a telnet client server with negotiations. Following is the code:
enter code here
#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>
char buf1[] = {0xff, 0xfb, 0x18, 0xff, 0xfb, 0x1f};
char buf2[] = {0xff, 0xfc, 0x20, 0xff, 0xfc, 0x23, 0xff, 0xfb, 0x27};
char buf3[] = {0xff, 0xfa, 0x1f, 0x00, 0x78, 0x00, 0x32, 0xff, 0xf0};
char buf4[] = {0xff, 0xfa, 0x27, 0x00, 0xff, 0xf0, 0xff, 0xfa, 0x18, 0x00, 0x41, 0x4e, 0x53, 0x49, 0xff, 0xf0};
char buf5[] = {0xff, 0xfd, 0x03};
char buf6[] = {0xff, 0xfb, 0x01, 0xff, 0xfe, 0x05, 0xff, 0xfc, 0x21};
char buf7[] = {0xff, 0xfc, 0x01};
char buf8[] = {0xff, 0xfd, 0x01};
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("%d bytes received buffer is: %s\n", n, buffer);
for (int i = 0; i < n; i++)
printf("%2x ", buffer[i]);
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);}
//printf("host %s, port %d\n", host.c_str(), portno);
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_addr.s_addr = inet_addr( host.c_str() ); // ( "127.0.0.1" );
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);
}
printf("Please enter the message=");
bzero(buffer,256);
fgets(buffer,255,stdin);
n= write(sockfd,buffer,strlen(buffer));
if(n<0)
printf("ERROR writing in socket %d len %d", n, strlen(buffer));
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if(n<0)
perror("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
buffer[0] = 0x0d;
buffer[1] = 0x0a;
mwrite ( sockfd, buffer, 2);
printf("read 1 ");
read(sockfd);
mwrite( sockfd, buf1, sizeof(buf1));
sleep(2);
mwrite( sockfd, buf2, sizeof(buf2));
printf("read 2 ");
read(sockfd);
mwrite( sockfd, buf3, sizeof(buf3));
printf("read 3a ");
read(sockfd);
sleep(2);
mwrite( sockfd, buf4, sizeof(buf4));
printf("read 3b ");
read(sockfd);
mwrite( sockfd, buf5, sizeof(buf5));
sleep(2);
mwrite( sockfd, buf6, sizeof(buf6));
printf("read 4 ");
read(sockfd);
mwrite( sockfd, buf7, sizeof(buf7));
sleep(2);
mwrite( sockfd, buf8, sizeof(buf8));
read(sockfd);
mwrite ( sockfd, buffer, 2);
read(sockfd);
return 0;
}
Server.cpp
enter code here :
#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 <unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
//unsigned clilen;
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)
{
error("ERROR opening socket");
}
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)
{
error("ERROR on binding");
}
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
error("ERROR on accept");
}
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
{
error("ERROR reading from socket");
}
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0)
{
error("ERROR writing to socket");
}
return 0;
}
Thanks in advance.

[It started to be too much for comments, so I write as an answer instead.]
As for your problem, it's actually not a single problem but two intermingled problems: Buffering and negotiation. You need buffering because TCP is a streaming protocol, a receive call might receive only a part of a telnet message, and a send call might send only a part of a telnet message. The second problem is the negotiation, for which you need a so-called state machine. And you need one state per option.
You also need to read and understand RFC854 and RFC855. See the Telnet Wikipedia article for a comprehensive list of RFC's.
For the option negotiation, WILL and WONT is responded by DO or DONT. You should generally not send a DO or DONT unless you already received a WILL or WONT. For starters, until you implemented proper handing of the options, I recommend you always answer with DONT whenever you get a WILL (and or course a WONT) message. Don't send WILL or WONT unless you really need to negotiate about an option.
More implementation-wise, I recommend four state tables, one each for WILL, WONT, DO and DONT that you have sent. Those tables contain simple boolean values that tells you if you have sent the corresponding message to the peer. These four table assumes that any unrecognized option you receive is replied with a DONT or WONT.
If you send a WILL then you mark that in the corresponding state-table, so that when you receive a DO or DONT for the option you know that it was you that initiated the negotiation.

Related

Linux UDP ephemeral port bind occasionally fails to receive

I am writing a set of tests that depend on two applications communicating (locally) with a UDP socket. These UDP sockets are originally bound using port 0, then the actual bound port is queried using getsockname and shared between the two applications. Occasionally, the subsequent recv calls fail to return any data without any error reporting during binding etc.
I've simplified my implementation to a bare-bones test an included it below. It fails with "Received failed, got -1 expeccted 6" about 1 of 10 runs. What am I missing in the socket setup to reliable use an ephemeral, OS-assigned port?
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <thread>
int32_t createSocket(uint16_t& rBoundPort)
{
int32_t s = socket(AF_INET, SOCK_DGRAM, 0);
if (s >= 0)
{
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeval)) < 0)
{
::close(s);
return -1;
}
int32_t r(1);
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(int)) < 0)
{
close(s);
return -1;
}
int32_t bufSize(50000);
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize)) < 0)
{
close(s);
return -1;
}
// Setup local listening port
sockaddr_in listenAddress;
memset(reinterpret_cast<char*>(&listenAddress), 0, sizeof(listenAddress));
listenAddress.sin_family = AF_INET;
inet_aton("127.0.0.1", &listenAddress.sin_addr);
listenAddress.sin_port = htons(0);
if (bind(s, (struct sockaddr*)&listenAddress, sizeof(listenAddress)) != 0)
{
close(s);
return -1;
}
// Update the bound listen port
socklen_t boundAddrLen = sizeof(sockaddr_in);
getsockname(s, (struct sockaddr*)&listenAddress, &boundAddrLen);
rBoundPort = ntohs(listenAddress.sin_port);
}
return s;
}
void mysockettest(int32_t s, uint16_t destPort)
{
const int32_t dataSize = 6;
char aWrite[dataSize] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char aRx[dataSize];
sockaddr_in dest;
memset((char*)&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
inet_aton("127.0.0.1", &dest.sin_addr);
dest.sin_port = htons(destPort);
sendto(s, aWrite, dataSize, 0, (struct sockaddr*)&dest, sizeof(sockaddr_in));
sockaddr_in src;
socklen_t srcSize = sizeof(sockaddr_in);
memset((char*)&src, 0, srcSize);
int32_t bytesReceived = recvfrom(s, aRx, dataSize, 0, (struct sockaddr*)&src, &srcSize);
if (bytesReceived != dataSize)
{
printf("Received failed, got %d expected %d\n", bytesReceived, dataSize);
}
}
int main(int argc, char** pargv)
{
uint16_t s1Port(0);
int32_t s1 = createSocket(s1Port);
if (s1 < 0)
{
printf("FAILED TO OPEN SOCKET 1\n");
return -1;
}
if (s1Port < 1)
{
printf("FAILED TO BIND SOCKET 1 TO PORT\n");
return -1;
}
uint16_t s2Port(0);
int32_t s2 = createSocket(s2Port);
if (s2 < 0)
{
printf("FAILED TOOPEN SOCKE 2\n");
}
if (s2Port < 1)
{
printf("FAILED TO BIND SOCKET 2 TO PORT\n");
}
std::thread t1(mysockettest, s1, s2Port);
std::thread t2(mysockettest, s2, s1Port);
t1.join();
t2.join();
close(s1);
close(s2);
}
```
Unforntunately I can't explain the details of they "why" (perhaps someone else can), but I found that adding a global mutex lock to protect the send corrected the instability on the send. The global lock isn't practical for my situation, so instead I implemeneted a simply retry loop on the send (if send() == -1, retry 10 times). While this solution is not ideal, it has proven stable.
Once that stability was resolved, I also saw occasional failures on the read side. With SO_REUSEADDR and binding with port 0 (OS-assignment), it was possible to have both sockets be assigned and bind to the same port, so I removed the SO_REUSEADDR when binding to port 0.

linux socket lose data when a delay is added before read

I am learning linux socket programming, I expect that server can read data, even I add a delay but it just drops the buffer data, and receive the recent data, that is why, Thanks. The code has been presented.
By the way, Could you show a common practice to deal with this kind of situation?
Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
const char hello[] = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
for (int i = 0;; i++)
{
sleep(5);
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
}
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
const char data[] = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
for (int i = 0;; i++)
{
sleep(1);
std::string hello = std::string(data) + std::to_string(i);
if (send(sock, hello.c_str(), hello.length() + 1, 0) != hello.length() + 1)
{
printf("error send %d \n", i);
}
printf("Hello message sent %d\n", i);
}
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);
return 0;
}
The problem is, that the messages get concatenated in the socket. The socket represents a byte stream. Your sender puts bytes into the stream every second. On the first iteration, it writes "Hello from client0\0" (19 bytes) to the stream.
After one second, it writes "Hello from client1\0", and then "Hello from client2\0", "Hello from client3\0" and "Hello from client4\0", Now, after 5 Seconds, 5*19 = 95 bytes are written to the byte stream.
Now, the receiver calls valread = read(new_socket, buffer, 1024);. Guess what, it reads all 95 bytes (because you specified 1024 as buffer size) and sets valread to 95.
Then you call printf("%s\n", buffer);, which only prints the first 18 bytes of buffer, because there is a '\0' as 19th byte, which terminates '%s' format. Allthough 95 bytes are received, 76 bytes are missing in the output of your program.
If you use '\n' instead of '\0' as message separator and use write(1, buffer, valread) instead of printf("%s\n") on the receiving side, you will see all your data.
std::string hello = std::string(data) + std::to_string(i) + "\n";
if (send(sock, hello.c_str(), hello.length(), 0) != hello.length()) ...
Conclusion:
Stream sockets realize byte sreams, the do not preserve message boundaries.
If message bounaries must be preserved, you need to use a protocol on top of the stream to mark your message boundaries. The proptocol could be as simple as using '\n' as a message seaparator, as long as '\n' is not part of your message payload (e.g. when unsign a simple text protocol).
You block the server for 5 seconds and it cannot receive some messages from the client.
for (int i = 0;; i++)
{
sleep(5);
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
}
How can a client check if the server is receiving a message? I think this was discussed in Linux socket: How to make send() wait for recv()
P.S. It looks like there is a synchronizing piece of code, but you pulled it out of the loop.
Server:
}
send(new_socket, hello, strlen(hello), 0);
Client:
}
valread = read(sock, buffer, 1024);

TCP/IP file transfer C++

I'm a beginner in programming in c++ with a bit of experience in java.
My goal is transfer files (in local host) between a server and a client (in two different folders).
I have found this code online that works well for me, it opens sockets and makes a connection between the server and the client and you can send a message typed in the terminal.
I have tried a lot of different codes on the INTERNET and either it doesn't compile or it isn't what I need.
Here is a code that I found that works to transmit a message.
server.cpp file
enter code here
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const 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) {
error("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) {
error("ERROR on binding");
}
listen(sockfd,1);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
client.cpp file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
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]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
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);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}
I'm compiling with g++ in ubuntu.
I'd love if you could help me to at least be able to send a txt file from the client or download a file from the server, as soon as i try to add functionalities in this code nothing works anymore, maybe I'm starting with a bad example?
you should try this example, it works good for me
receive:
size_t datasize;
FILE* fd = fopen(filename, "wb");
while (!received)
{
datasize = recv(connection, text, sizeof(text), 0);
fwrite(&text, 1, datasize, fd);
}
fclose(fd);
send:
FILE *fd = fopen(filename, "rb");
size_t rret, wret;
int bytes_read;
while (!feof(fd)) {
if ((bytes_read = fread(&buffer, 1, BUFFER_SIZE, fd)) > 0)
send(sock, buffer, bytes_read, 0);
else
break;
}
fclose(fd);
Good luck:)

Unable to close server side socket

Following is a single server-client code which are trying to send-receive few buffers, and later close the connection.
Server Code:
#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>
char buff1[][3]= {{0xff , 0xfd, 0x18},{0xff , 0xfd, 0x1e},{0xff , 0xfd, 0x1d}};
char recbuf[1024];
void mwrite (int sock, char * buf, int size)
{
int n = send( sock, buf, size, 0 );
if (n < 0)
{
perror("ERROR writing to socket");
return;
}
}
void read (int sock)
{
char buffer[256];
/* Now read client response */
memset(buffer, 0, sizeof(buffer));
int n = recv( sock, buffer, 255, 0 );
if (n < 0)
{
perror("ERROR reading from socket");
return;
}
for (int i = 0; i < n; i++)
printf("%2x ", buffer[i]);//printing ascii characters
printf("\n");
}
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;
sockfd = socket(AF_INET , SOCK_STREAM , 0);
if (sockfd < 0 )
{
perror("Error opening socket ");
exit(1);
}
/* Initialize socket structure */
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);
/* Now bind the host address using client */
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");
exit(1);
}
int count = 1;
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
printf("Server Sending (DO TERMINAL TYPE)\n");
while ((n = write(newsockfd,buff1,sizeof(buff1)))>0)
{
printf("Server Sent query %d: %x %x %x\n", count, buff1[count][0], buff1[count][1], buff1[count][2]);
n = read(newsockfd, recbuf , sizeof(recbuf));
printf("Server received responset: %x %x %x\n", recbuf[0], recbuf[1], recbuf[2]);
count++;
count = count % 3;
if(n<0)
{
perror("Error writing to socket ");
exit(1);
}
}
return 0 ;
}
Client Code :
#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>
#include <netdb.h>
char buff2[] = {0xff,0xfc,0x18};
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);
}
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 *server1;
char buffer1[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);
}
server1 = gethostbyname(argv[1]);
if (server1 == 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 *)server1->h_addr, (char*)&serv_addr.sin_addr.s_addr, server1->h_length);
serv_addr.sin_port = htons( portno );
/*Connect to server*/
if (connect( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0)
{
perror("ERROR connecting");
return(1);
}
printf("Recieving Buffer 1 from Server side");
bzero(buffer1,256);
while(true)
{
n= read(sockfd,buffer1,sizeof(buffer1));
if(n<0)
printf("ERROR reading in socket %d len %d", n, sizeof(buffer1));
n= write(sockfd,buff2,sizeof(buff2));
if(n<0)
printf("ERROR writing in socket %d len %d", n, sizeof(buff2));
printf("\nSent Buffer2 (WON'T TERMINAL TYPE) from client side");
}
return 0;
}
Output :
Server Side :
./single_sample 5667
Server Sending (DO TERMINAL TYPE)
Server Sent query 1: ffffffff fffffffd 1e
Server received responset: ffffffff fffffffc 18
Server Sent query 2: ffffffff fffffffd 1d
Server received responset: ffffffff fffffffc 18
Error writing to socket : Connection reset by peer
Client Side:
./sample_client 127.0.0.1 5667
Recieving Buffer 1 from Server side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side
Basically I am trying to close the socket once both the buffers are sent. But getting the above error. I am unable to solve whether the error is occuring from server side or client side.
Also, while running this code on a linux machine how to capture the data bytes sent on wireshark
Thanks in advance.
Your client writes its message and then immediately disconnects, which leads the server to get the Connection reset by peer error - since the client part of the connection no longer exists.

How to send Queries from Server and receive echo from client using Telnet libraries

I have written a socket program wherein the client sends Telnet negotiations stored in char buffers to the server. The server receives it and responds with the message length of each buffer.Here is the code :
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;
Output is :
Client output :
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
Server output:
#debian:~/echoserver$ ./server 8088
Recieved : .N=��
Recieved :
=��
I would also like to know if we want to reverse the process, that is ,send a 3 byte query sequence from server to client, where byte1-Interpret as command(0xff) , byte2-command code , byte3 - option code. Telnet is streamed so a received message may contain one or more queries. For example, the server sends a 3 byte message upon accepting connection (ff , fd , 18 ). The client should just echo back with Won't(ff , fc , 18)
Eg:
Server : Sending Query 1 : 0xff,0xfd,0x18
Client echo : 0xff , 0xfc , 0x18
Thanks in advance.
Your buf arrays are not strings (no terminating '\0' character), but you call strlen() on them. That will give you undefined behavior. Since they are binary arrays, use sizeof instead.
This is clearly visible in your own trace printf()s, the first line says "Bytes Sent: 18" but clearly buf1 has only 6 bytes in it.
In client.cpp, buffer is not initialized before its first use.
int main(int argc, char *argv[]) {
char buffer[256];
...
n= write(sockfd,buffer,strlen(buffer));
When receiving a "string" from a socket, eithe append a trialing '\0' or insure the sender hase sent it.
n = write(sockfd,buffer,strlen(buffer)+1);
...
n = read(sockfd, buffer, sizeof buf - 1);
if (n > 0 && buffer[n-1] != '\0') {
buffer[n-1] = '\0';
n++;
}
Function signature do not line up in client.cpp, so even though this is tagged C, this is a C++ post.
void read (int sock) { ... }
int main(int argc, char *argv[]) {
...
n = read(sockfd, buffer, 255);