2017年6月9日 星期五

DOS network

https://www.lazybrowndog.net/freedos/virtualbox/

上面是教導如何在 DOS 使用網路並存取網路磁碟機的方法
經實測 DOS <-> Windows 7 確定是可以互通的

另外如果是想自己寫程式發送 TCP or UDP 封包
可以考慮  http://www.brutman.com/mTCP/ 提供的 mTCP-src_2013-05-23.zip
並參考 \APPS\HTGET 這支範例

編譯方式也很簡單
1. 開啟 Open Watcom Build Environment cmd windows
2. cd \APPS\HTGET
3. wmake -f MAKEFILE

據實測結果,在另一台電腦用 Go 寫的 TCPServer 確定可以收到封包
不過常常送了幾次後,DOS 端的 socket 就會關閉
目前還沒有時間研究這個,等以後再來查看


TCPServer - Golang

package main

import "net"
import "fmt"

func main() {
    port := 8080

    fmt.Printf("Launching TCP server port %d\n", port)

    ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    if err != nil {
        fmt.Println(err)
        return
    }

    for {
        conn, err := ln.Accept()
        if err != nil {
            fmt.Println(err)
            continue
        }

        go handleRequest(conn)
    }
}

func handleRequest(conn net.Conn) {
    defer conn.Close()

    remoteIP := conn.RemoteAddr().String()

    buf := make([]byte, 1024)

    fmt.Println("--------------------")
    fmt.Println(remoteIP)

    for {
        reqLen, err := conn.Read(buf)
        if err != nil {
            //fmt.Println(err)
            break
        }

        if reqLen > 0 {
            fmt.Printf("%s(%d)\n", string(buf[:reqLen]), reqLen)
        }
    }

    conn.Close()

    fmt.Println("socket closed")
}


TCPSend - DOS (僅列出修改部份,其他都是原本的)

int sendTCP(char *msg, int len)
{
  int sendLen = 0;
  int rc;

  while (sendLen < len)
  {
    rc = sock->send( (uint8_t *)(msg+sendLen), len-sendLen);
    PACKET_PROCESS_SINGLE;
    Arp::driveArp( );
    Tcp::drivePackets( );

    if (rc > 0) {
      sendLen += rc;
    }
    else if ( rc == 0 ) {
      // Out of send buffers maybe?  Loop around to process packets
      printf("sock->send error ?\n");
      return -1;
    }
    else {
      return -1;
    }
  }

  return 0;
}

int main( int argc, char *argv[] ) {

  // Initialize TCP/IP
  if ( Utils::parseEnv( ) != 0 ) {
    exit(1);
  }

  if ( Utils::initStack( 1, TCP_SOCKET_RING_SIZE ) ) {
    fprintf( stderr, "\nFailed to initialize TCP/IP - exiting\n" );
    exit(1);
  }

  // From this point forward you have to call the shutdown( ) routine to
  // exit because we have the timer interrupt hooked.

  // Start
  Utils::Debugging |= 0xff;
  ServerPort = 8080;
  strcpy(Hostname, "192.168.11.9");

  fprintf( stderr, "Server: %s:%u\n", Hostname, ServerPort);

  if ( resolve(Hostname, HostAddr) ) shutdown( 1 );

  if ( connectSocket( ) ) shutdown( 1 );


  // send
  int rc;
  char data[64];
  int sent_bytes;
  int sendstrlen;

  printf("input message to send or 'q' to exit \n\n");

  while (1)
  {
    memset(data, 0x00, sizeof(data));
    fgets(data, sizeof(data) - 1, stdin);
    if (data[0] == 'q')
    {
      printf("ready to exit connection\n");
      break;
    }

    sendstrlen = strlen(data);
    if (sendstrlen > 0 && sendstrlen < 64)
    {
      rc = sendTCP(data, sendstrlen);
      printf("rc = %d \n", rc);
    }
  }

  shutdown( rc );
}

沒有留言:

張貼留言