This thing many friends have written, I also write to play, as a note it. No more nonsense.
TCP/IP is widely used in data communication, it has to include the client and the server, of course, self-talk is not what can not, but that seems a bit nerve.
Well, let's start with the service side.
1, create a new project, take the name MyServer bar, and then typed the following code:
Using System;
Using System.Net;
Using System.Net.Sockets;
Namespace MyServer
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Server service started ...");
IPAddress IP = dns.gethostentry ("localhost"). ADDRESSLIST[0];
TcpListener listener = new TcpListener (IP, 250 );
Listener. Start (); Start listening on
Console.WriteLine ("Start listening ...");
Console.WriteLine ("\ n input \" q\ "key exits.) ");
Consolekey key;
Do
{
Key = Console.readkey (true). Key;
}
while (Key! = CONSOLEKEY.Q);
}
}
}
Well, run ... The display service is started.
Next, let's see if our listening port number 250 is already being monitored. Such as:
2, good, everything is normal. Let's start by writing the client.
Reopen a vs window, create a new project MyClient, and enter the following code:
Using System;
Using System.Net.Sockets;
Namespace MyClient
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Client-initiated ...");
TcpClient client = new TcpClient ();
Try
{
Connect to Server
Client. Connect ("localhost", 250);
}
catch (Exception ex)
{
Console.WriteLine (ex. Message);
Return
}
Print the service-side information connected to
Console.WriteLine ("The connection has been successfully established with the client!" {0} and {1} ", client. Client.localendpoint, client. Client.remoteendpoint);
Console.WriteLine ("\ n input \" q\ "key exits.) ");
Consolekey key;
Do
{
Key = Console.readkey (true). Key;
}
while (Key! = CONSOLEKEY.Q);
}
}
}
It seems like everything is so clear, OK, start running No reaction? Hit a breakpoint and try to track it?
Did you see that anomaly? "Unable to connect because the target computer was actively rejected. 127.0.0.1:250 "
Damn, my target computer is so positive, huh? Is it a good thing the firewall is doing? Off--No, just the same. is the port number occupied? Can't use 250? What if I change 2500? Try a few more ...:-D:-D is still not possible, stating that there is no port number. That program does not have enough permission to run? I'll try it with the administrator--or not. This thing is not as good as WCF so worry Ah!
OK, I'll change this localhost to an IP address and try it. First through the Ipconfig to see what their IP address is, the amount, 192.168.175.1, all right, all changed.
The IP of the server is replaced by:
IPAddress IP = ipaddress.parse ("192.168.175.1");
The client is replaced by:
Client. Connect (System.Net.IPAddress.Parse ("192.168.175.1"), 2500);
Again run try, pass the pass, can ah! See:
Oh ah, yes, see the two port numbers do not? The first one comes from the client, which is randomly assigned by the client itself, and the back 2500 is the port number of the server we are listening on (put 250-->2500)
In fact, TCP/IP this thing Ah, server and client, really want in the vast crowd, find each other not easy ah. Even if you know where her family is, and the keys to their home, if you're on your way to their house and you're stuck in a path, you won't be able to let you through. So, in order to get to their home, we have to think of other ways. That is ..... When I'm free, I'll write about how to make sure you get to their house.
C # TCP/IP programming