I'm trying to learn a little about socket programming, and I came across TcpListener and TcpClient to use as I read that they are a bit easier for beginners. The main point of what I want to do is to have a small form that can be run on my laptop and on another laptop on the same network and so that they can communicate, that is, send a line of text to each other. As soon as I have it, I hope that it will develop further :)
So far I have created both a client and a server program using msdn and various manuals found on the Internet. I can make them communicate when they are working on one laptop, however, when I move the client to another laptop, I will not go anywhere. I think my main problem is that I donβt quite understand how the client finds the server IP address, because I think I can hardcode it, but when I return at another time, Iβm sure that the IP address will change. Is there a way to get them to connect more dynamically to cover a changing IP address? My current client code is:
public void msg(string mesg) { lstProgress.Items.Add(">> " + mesg); } private void btnConnect_Click(object sender, EventArgs e) { string message = "Test"; try {
My current server code is:
private void Prog_Load(object sender, EventArgs e) { bw.WorkerSupportsCancellation = true; bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); if (bw.IsBusy != true) { bw.RunWorkerAsync(); } } private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { lstProgress.Items.Add(e.UserState); } private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if ((worker.CancellationPending == true)) { e.Cancel = true; } else { try { // Set the TcpListener on port 1333. Int32 port = 1333; //IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener server = new TcpListener(IPAddress.Any, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { bw.ReportProgress(0, "Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); bw.ReportProgress(0, "Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); bw.ReportProgress(0, String.Format("Received: {0}", data)); // Process the data sent by the client. data = String.Format("I Have Received Your Message: {0}", data); byte[] mssg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(mssg, 0, mssg.Length); bw.ReportProgress(0, String.Format("Sent: {0}", data)); } // Shutdown and end connection client.Close(); } } catch (SocketException se) { bw.ReportProgress(0, String.Format("SocketException: {0}", se)); } } }
As you can probably say I'm new to this, so if there is a better way to implement this, I am more than happy to find out! Thanks for any help in advance :)
My solution thanks to the answers below:
private String IPAddressCheck() { var IPAddr = Dns.GetHostEntry("HostName"); IPAddress ipString = null; foreach (var IP in IPAddr.AddressList) { if(IPAddress.TryParse(IP.ToString(), out ipString) && IP.AddressFamily == AddressFamily.InterNetwork) { break; } } return ipString.ToString(); } private void btnConnect_Click(object sender, EventArgs e) { string message = "Test"; try { Int32 port = 1337; string IPAddr = IPAddressCheck(); TcpClient client = new TcpClient(IPAddr, port);
I'm not sure if this is the best solution, but it works well, so thanks for the answers :)