Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///The TCP Client
- using System;
- using System.Net.Sockets;
- using System.Text;
- namespace WindowsFormsApplication1
- {
- public class TcpTimeClient
- {
- private const int portNum = 8124;
- private const string hostName = "192.168.7.119";
- private static TcpClient client = new TcpClient(hostName, portNum);
- private static string serverMessage = "";
- public static int start()
- {
- try
- {
- NetworkStream ns = client.GetStream();
- byte[] bytes = new byte[1024];
- int bytesRead = ns.Read(bytes, 0, bytes.Length);
- serverMessage = (Encoding.ASCII.GetString(bytes, 0, bytesRead));
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- return 0;
- }
- public static string getMessage()
- {
- return serverMessage;
- }
- public static void send(string message)
- {
- try
- {
- NetworkStream ns = client.GetStream();
- byte[] bytes = Encoding.ASCII.GetBytes(message);
- ns.Write(bytes, 0, bytes.Length);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- public static void close()
- {
- client.Close();
- }
- }
- }
- ///Web Form Code
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class nodeForm : Form
- {
- public nodeForm()
- {
- InitializeComponent();
- chatWindow.Text += "Connecting...\r\n";
- string str = TcpTimeClient.getMessage();
- chatWindow.Text += str;
- }
- private void retrieveInput_Click(object sender, EventArgs e)
- {
- string str = inputTextBox.Text;
- inputTextBox.Text = "";
- chatWindow.Text += "Me: " + str + "\r\n";
- TcpTimeClient.send(str);
- }
- private void nodeForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- TcpTimeClient.close();
- }
- }
- }
- ///Program Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- TcpTimeClient.start();
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new nodeForm());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement