i'll start typing a command in the telnet module to communicate with my telnet server after i've already been connected and the server will suddenly drop me, exit telnet, and bring up the cmd prompt. what in this code is doing this?

void do_handleclient(int connsock,struct sockaddr_in *client_addr) {
  // Communicating with our new friend.
  string buffer, execmd;
  char filename[25000];
  int b=0, n=1, nstr=0, str=0;
  string sendbuf;
  char *cbuff=(char*)sendbuf.c_str();

  while(n!=0)
	  n=recv(connsock,cbuff,25000-1,0);
  while(cbuff[nstr]!=' ') { 
	  execmd[nstr]=cbuff[nstr];
	  nstr++;
  }
  while(cbuff[nstr]!='\r' || cbuff[nstr]!=' ') {
	  filename[str]=cbuff[str];
	  nstr++;
  }
  
  ifstream fd;
  fd.open(filename, ifstream::in);
  int length;
  while(1) {
      fd.read(cbuff,25000); // Reading the textfile.
	  fd.seekg(0,ios::end);
	  length=fd.tellg();
	  fd.seekg(0,ios::beg);
	  //if(length=0)               // EOF.
	  //    break;
      if(send(connsock,cbuff,length,0)==-1) 
          break;
  }
}

Dani AI

Generated

Quick diagnosis for : the crash/disconnect behavior you see is classic undefined behavior and protocol-handling bugs. The code takes a pointer to sendbuf.c_str() and then treats it as writable, indexes into uninitialized std::string objects, uses the wrong boolean in the parse loop (|| instead of &&), and loops on recv with while(n!=0) n=recv(...) which either discards data or never waits for a complete command. The file-send logic also seeks to the end after each read and hands an incorrect length to send. Any one of those will corrupt memory, return errors from send, trigger SIGPIPE, or make telnet exit immediately.

Practical fixes (short checklist) — implement all of these:

  • Use a writable buffer (e.g., std::vector<char> or a local char[]) for recv/file reads; never write into c_str() memory.
  • Check recv() return: >0 bytes read, ==0 peer closed, <0 error (inspect errno/WSAGetLastError).
  • Parse safely: accumulate bytes into a std::string and find() the space and CRLF; do bounds checks before indexing.
  • Open files as binary, read fixed-size chunks, use .gcount() to get bytes read.
  • Loop on send() until all bytes are sent (handle partial sends); handle SIGPIPE or use platform flag to avoid process termination.

Example sketch (illustrative):

void handle_client_socket(int sock) {
    std::vector<char> buf(8192);
    std::string acc;
    int n;
    while ((n = recv(sock, buf.data(), (int)buf.size(), 0)) > 0) {
        acc.append(buf.data(), n);
        auto eol = acc.find("\r\n");
        if (eol == std::string::npos) continue;
        std::string line = acc.substr(0, eol);
        auto sp = line.find(' ');
        if (sp == std::string::npos) break;
        std::string filename = line.substr(sp+1);
        std::ifstream f(filename, std::ios::binary);
        if (!f) { send(sock, "File not found\r\n", 16, 0); break; }
        std::vector<char> fbuf(4096);
        while (f.read(fbuf.data(), fbuf.size()) || f.gcount()) {
            int tosend = (int)f.gcount(), sent = 0;
            while (sent < tosend) {
                int s = send(sock, fbuf.data()+sent, tosend-sent, 0);
                if (s <= 0) return;
                sent += s;
            }
        }
        break;
    }
    // handle n==0 and n<0, then close socket appropriately
}

Troubleshooting notes: run under Valgrind/ASAN or a debugger to catch invalid writes; log recv/send return values and errno. For : if LAN works but not internet, check binding (0.0.0.0), firewall, router port-forward, and test with netcat/tcpdump; telnet clients also send IAC negotiation bytes — if you only expect simple text, strip or ignore telnet control sequences.

I can send messages on lan on local but I cann't send messages on internet on global. please help me. do you have a source of sending messages on global on internet?

anyone?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.