Advertisement
Guest User

Untitled

a guest
Aug 20th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. ///The TCP Client
  2.  
  3. using System;
  4. using System.Net.Sockets;
  5. using System.Text;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9.     public class TcpTimeClient
  10.     {
  11.         private const int portNum = 8124;
  12.         private const string hostName = "192.168.7.119";
  13.         private static TcpClient client = new TcpClient(hostName, portNum);
  14.         private static string serverMessage = "";
  15.  
  16.         public static int start()
  17.         {
  18.             try
  19.             {                
  20.                 NetworkStream ns = client.GetStream();
  21.                 byte[] bytes = new byte[1024];
  22.                 int bytesRead = ns.Read(bytes, 0, bytes.Length);
  23.  
  24.                 serverMessage = (Encoding.ASCII.GetString(bytes, 0, bytesRead));
  25.             }
  26.             catch (Exception e)
  27.             {
  28.                 Console.WriteLine(e.ToString());
  29.             }
  30.  
  31.             return 0;
  32.         }
  33.  
  34.         public static string getMessage()
  35.         {
  36.             return serverMessage;
  37.         }
  38.  
  39.         public static void send(string message)
  40.         {
  41.             try
  42.             {
  43.                 NetworkStream ns = client.GetStream();
  44.                 byte[] bytes = Encoding.ASCII.GetBytes(message);
  45.                 ns.Write(bytes, 0, bytes.Length);
  46.             }
  47.             catch (Exception e)
  48.             {
  49.                 Console.WriteLine(e.ToString());
  50.             }
  51.         }
  52.  
  53.         public static void close()
  54.         {
  55.             client.Close();
  56.         }
  57.     }
  58. }
  59.  
  60. ///Web Form Code
  61.  
  62. using System;
  63. using System.Collections.Generic;
  64. using System.ComponentModel;
  65. using System.Data;
  66. using System.Drawing;
  67. using System.Linq;
  68. using System.Text;
  69. using System.Threading.Tasks;
  70. using System.Windows.Forms;
  71.  
  72. namespace WindowsFormsApplication1
  73. {
  74.     public partial class nodeForm : Form
  75.     {
  76.         public nodeForm()
  77.         {
  78.             InitializeComponent();
  79.             chatWindow.Text += "Connecting...\r\n";
  80.             string str = TcpTimeClient.getMessage();
  81.             chatWindow.Text += str;
  82.         }
  83.  
  84.         private void retrieveInput_Click(object sender, EventArgs e)
  85.         {
  86.             string str = inputTextBox.Text;
  87.             inputTextBox.Text = "";
  88.             chatWindow.Text += "Me:  " + str + "\r\n";
  89.             TcpTimeClient.send(str);
  90.         }
  91.  
  92.         private void nodeForm_FormClosing(object sender, FormClosingEventArgs e)
  93.         {
  94.             TcpTimeClient.close();          
  95.         }
  96.     }
  97. }
  98.  
  99. ///Program Code
  100.  
  101. using System;
  102. using System.Collections.Generic;
  103. using System.Linq;
  104. using System.Threading.Tasks;
  105. using System.Windows.Forms;
  106.  
  107. namespace WindowsFormsApplication1
  108. {
  109.     static class Program
  110.     {
  111.         /// <summary>
  112.         /// The main entry point for the application.
  113.         /// </summary>
  114.         [STAThread]
  115.         static void Main()
  116.         {
  117.             TcpTimeClient.start();
  118.             Application.EnableVisualStyles();
  119.             Application.SetCompatibleTextRenderingDefault(false);
  120.             Application.Run(new nodeForm());
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement