Using system;
Using system. Collections. Generic;
Using system. text;
Using system. net;
Using system. net. Sockets;
Namespace udpserver
{
Class Program
{
Static void main (string [] ARGs)
{
Int Recv;
Byte [] DATA = new byte [1024];
// Construct a TCP Server
// Obtain the local IP address and set the TCP port number
Ipendpoint ipep = new ipendpoint (ipaddresses. Any, 8001 );
Socket newsock = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
// Bind the network address
Newsock. BIND (ipep );
Console. writeline ("this is a server, host name is {0}", DNS. gethostname ());
// Wait for the client to connect
Console. writeline ("waiting for a client ");
// Obtain the Client IP Address
Ipendpoint sender = new ipendpoint (IPaddress. Any, 0 );
Endpoint remote = (endpoint) (sender );
Recv = newsock. receivefrom (data, ref remote );
Console. writeline ("Message received ed from {0}:", remote. tostring ());
Console. writeline (encoding. ASCII. getstring (data, 0, Recv ));
// Send the welcome message after the client connection is successful
String welcome = "Welcome! ";
// String and byte array Conversion
Data = encoding. ASCII. getbytes (welcome );
// Send information
Newsock. sendto (data, data. length, socketflags. None, remote );
While (true)
{
Data = new byte [2, 1024];
// Send acceptance Information
Recv = newsock. receivefrom (data, ref remote );
Console. writeline (encoding. ASCII. getstring (data, 0, Recv ));
Newsock. sendto (data, Recv, socketflags. None, remote );
}
}
Static void send (string [] ARGs)
{
Byte [] DATA = new byte [1024];
String input, stringdata;
// Construct a TCP Server
Console. writeline ("this is a client, host name is {0}", DNS. gethostname ());
// Set the service IP address and TCP port number
Ipendpoint ipep = new ipendpoint (IPaddress. parse ("127.0.0.1"), 8001 );
// Define the network type, data connection type, and network protocol UDP
Socket server = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
String welcome = "Hello! ";
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 [2, 1024];
// Add this line to a nonexistent IP Address Code You can remove the blocking mode within the specified time.
// Server. setsocketoption (socketoptionlevel. socket, socketoptionname. receivetimeout, 100 );
Int Recv = server. receivefrom (data, ref remote );
Console. writeline ("Message received ed from {0}:", remote. tostring ());
Console. writeline (encoding. ASCII. getstring (data, 0, Recv ));
While (true)
{
Input = console. Readline ();
If (input = "exit ")
Break;
Server. sendto (encoding. ASCII. getbytes (input), remote );
Data = new byte [2, 1024];
Recv = server. receivefrom (data, ref remote );
Stringdata = encoding. ASCII. getstring (data, 0, Recv );
Console. writeline (stringdata );
}
Console. writeline ("Stopping client .");
Server. Close ();
}
}
}