Home>

First, udp way:

Server-side code:

static void main (string [] args)
{
  int recv;
  byte [] data=new byte [1024];
  ipendpoint ipep=new ipendpoint (ipaddress.any, 9050);//Define a network endpoint
  socket newsock=new socket (addressfamily.internetwork, sockettype.dgram, protocoltype.udp);//Define a socket
  newsock.bind (ipep);//socket is associated with a local endpoint
  console.writeline ("waiting for a client ..");
  ipendpoint sender=new ipendpoint (ipaddress.any, 0);//Define the address of the computer to send
  endpoint remote=(endpoint) (sender);//
  recv=newsock.receivefrom (data, ref remote);//Accept data
  console.writeline ("message received from {0}:", remote.tostring ());
  console.writeline (encoding.ascii.getstring (data, 0, recv));
  string welcome="welcome to my test server!";
  data=encoding.ascii.getbytes (welcome);
  newsock.sendto (data, data.length, socketflags.none, remote);
  while (true)
  {
    data=new byte [1024];
    recv=newsock.receivefrom (data, ref remote);
    console.writeline (encoding.ascii.getstring (data, 0, recv));
    newsock.sendto (data, recv, socketflags.none, remote);
  }
}

Client code:

void maininfo ()
{
  byte [] data=new byte [1024];//Define an array as a buffer for data
  string input, stringdata;
  ipendpoint ipep=new ipendpoint (ipaddress.parse ("192.168.1.21"), 9050);
  socket server=new socket (addressfamily.internetwork, sockettype.dgram, protocoltype.udp);
  string welcome="hello, are you there?";
  data=encoding.ascii.getbytes (welcome);
  server.sendto (data, data.length, socketflags.none, ipep);
  ipendpoint sender=new ipendpoint (ipaddress.any, 0);
  endpoint remote=(endpoint) sender;
  data=new byte [1024];
  int recv=server.receivefrom (data, ref remote);//Accept data from the server
  console.writeline ("message received from {0}:", remote.tostring ());
  console.writeline (encoding.ascii.getstring (data, 0, recv));
  while (true) //Read data
  {
    input=richtextbox1.text;//Read data from the keyboard
    if (input == "text") //End tag
    {
      break;
    }
    server.sendto (encoding.ascii.getbytes (input), remote);//Send data to the specified endpoint remote
    data=new byte [1024];
    recv=server.receivefrom (data, ref remote);//Receive data from remote
    stringdata=encoding.ascii.getstring (data, 0, recv);
    console.writeline (stringdata);
  }
  console.writeline ("stopping client");
  server.close ();
}

TCP method:

Server-side code:

socket serversocket=null;
thread clientthread=null;
socket clientsocket=null;
thread thread=null;
ipaddress ips=null;
pendpoint ipep=null;
void serverstart ()
{
  ips=dns.gethostaddresses (dns.gethostname ()) [0];
  //Create an ipendpoint instance
  ipep=new ipendpoint (ips, 9050);
  //Create a socket
  serversocket=new socket (addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
  serversocket.setsocketoption (socketoptionlevel.socket, socketoptionname.reuseaddress, true);
  //bind the created socket to ipodpoint
  serversocket.bind (ipep);
  //Set the socket to listening mode
  serversocket.listen (20);
  while (listenalive)
  {
    try
    {
      //Receive incoming connection on socket
      clientsocket=serversocket.accept ();
      clientthread=new thread (new parameterizedthreadstart (receivedata));
      clientthread.start (clientsocket);
    }
    catch (exception ex)
    {
      writeerrorlog (ex.message);
      serversocket.close ();
      serversocket=null;
    }
  }
}
static void receivedata (object obj)
{
  bool keepalive=true;
  socket s=obj as socket;
  byte [] buffer=new byte [1024];
  //Send information to the client based on the listening client socket
  ipendpoint clientep=(ipendpoint) s.remoteendpoint;
  console.writeline ("client ip:" + clientep.address + "port:" + clientep.port);
  string welcome="Successfully connected to the server";
  buffer=system.text.encoding.unicode.getbytes (welcome);
  //Send a "connection to the server" message to the client
  s.send (buffer, buffer.length, socketflags.none);
  buffer=new byte [1024];
  int buflen=0;
  string content=string.empty;
  while (true)
  {
    //Receive information sent by the client on the socket
    buflen=0;
    try
    {
      buflen=s.receive (buffer);
      if (buflen == 0)
      {
        break;
      }
      content +=system.text.encoding.unicode.getstring (buffer, 0, buflen);
    }
    catch (exception ex)
    {
      break;;
    }
  }
  send (s, content);
  s=null;
  buffer=null;
  clientep=null;
  thread.currentthread.abort ();
}

Client code:

void send (string content)
{
  byte [] data=new byte [1024];
  newclient=new system.net.sockets.socket (system.net.sockets.addressfamily.internetwork, system.net.sockets.sockettype.stream, system.net.sockets.protocoltype.tcp);
  ie=new system.net.ipendpoint (system.net.ipaddress.parse (ipadd), port);//IP and port of the server
  try
  {
    //Because the client is only used to send information to a specific server,So there is no need to bind the local IP and port. No listening is required.
    newclient.connect (ie);
  }
  catch (system.net.sockets.socketexception e)
  {
    console.writeline (e.tostring ());
    return;
  }
  int recv=newclient.receive (data);
  //Successfully connected to the server
  string stringdata=system.text.encoding.unicode.getstring (data, 0, recv);
  if (stringdata == "Successfully connected to the server")
  {
    newclient.send (system.text.encoding.unicode.getbytes (content));
    newclient.shutdown (system.net.sockets.socketshutdown.send);
    data=new byte [1024];
    recv=newclient.receive (data);
    string result=system.text.encoding.unicode.getstring (data, 0, recv);
    newclient.shutdown (system.net.sockets.socketshutdown.receive);
    newclient.close ();
    messagebox.show (result);
  }
  else
  {
    messagebox.show ("Failed to connect to the server", "Friendship prompt");
  }
}
c
  • Previous Detailed explanation of ssh installation and scp command under linux
  • Next Detailed explanation of ajax request json data