Socket programming in C #-udp

Source: Internet
Author: User
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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.