Advertisement
Guest User

Untitled

a guest
May 17th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Threading;
  8. using System.Net.Sockets;
  9. using System.IO;
  10. //-----------------------------------------------------------
  11. //-----------------------------------------------------------
  12.  
  13. namespace Server
  14. {
  15. /// <summary>
  16. /// Summary description for Form1.
  17. /// </summary>
  18. public class Form1 : System.Windows.Forms.Form
  19. {
  20. //private AxACTIVESKINLib.AxSkin axSkin1;
  21. private System.Windows.Forms.TextBox tmsg;
  22. private System.Windows.Forms.Button bsend;
  23. private System.Windows.Forms.GroupBox groupBox2;
  24. private System.Windows.Forms.GroupBox groupBox3;
  25.  
  26. //--------------server connection variables ----------
  27. private Thread read;
  28. private NetworkStream stream;
  29. private BinaryReader reader;
  30. private BinaryWriter writer;
  31. private TcpListener listener;
  32. private Socket client;
  33. private System.Windows.Forms.PictureBox Box1;
  34. private System.Windows.Forms.RichTextBox tchat;
  35. //----------------------------------------------------
  36. private System.ComponentModel.Container components = null;
  37.  
  38. public Form1()
  39. {
  40. InitializeComponent();
  41. read=new Thread(new ThreadStart(RunServer));
  42. // read .s
  43. read.Start();
  44. }
  45.  
  46. /// <summary>
  47. /// Clean up any resources being used.
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null)
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60.  
  61. #region Windows Form Designer generated code
  62. /// <summary>
  63. /// Required method for Designer support - do not modify
  64. /// the contents of this method with the code editor.
  65. /// </summary>
  66. private void InitializeComponent()
  67. {
  68. this.tmsg = new System.Windows.Forms.TextBox();
  69. this.bsend = new System.Windows.Forms.Button();
  70. this.groupBox2 = new System.Windows.Forms.GroupBox();
  71. this.tchat = new System.Windows.Forms.RichTextBox();
  72. this.groupBox3 = new System.Windows.Forms.GroupBox();
  73. this.Box1 = new System.Windows.Forms.PictureBox();
  74. this.groupBox2.SuspendLayout();
  75. ((System.ComponentModel.ISupportInitialize)(this.Box1)).BeginInit();
  76. this.SuspendLayout();
  77. //
  78. // tmsg
  79. //
  80. this.tmsg.Location = new System.Drawing.Point(24, 256);
  81. this.tmsg.Name = "tmsg";
  82. this.tmsg.Size = new System.Drawing.Size(320, 20);
  83. this.tmsg.TabIndex = 0;
  84. this.tmsg.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tmsg_KeyPress);
  85. //
  86. // bsend
  87. //
  88. this.bsend.BackColor = System.Drawing.SystemColors.HighlightText;
  89. this.bsend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  90. this.bsend.Location = new System.Drawing.Point(152, 296);
  91. this.bsend.Name = "bsend";
  92. this.bsend.Size = new System.Drawing.Size(48, 24);
  93. this.bsend.TabIndex = 3;
  94. this.bsend.Text = "&Send";
  95. this.bsend.UseVisualStyleBackColor = false;
  96. this.bsend.Click += new System.EventHandler(this.bsend_Click);
  97. //
  98. // groupBox2
  99. //
  100. this.groupBox2.Controls.Add(this.tchat);
  101. this.groupBox2.Location = new System.Drawing.Point(12, 64);
  102. this.groupBox2.Name = "groupBox2";
  103. this.groupBox2.Size = new System.Drawing.Size(340, 160);
  104. this.groupBox2.TabIndex = 5;
  105. this.groupBox2.TabStop = false;
  106. this.groupBox2.Text = "groupBox2";
  107. //
  108. // tchat
  109. //
  110. this.tchat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
  111. this.tchat.Location = new System.Drawing.Point(8, 24);
  112. this.tchat.Name = "tchat";
  113. this.tchat.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
  114. this.tchat.Size = new System.Drawing.Size(320, 120);
  115. this.tchat.TabIndex = 0;
  116. this.tchat.Text = "";
  117. //
  118. // groupBox3
  119. //
  120. this.groupBox3.Location = new System.Drawing.Point(12, 240);
  121. this.groupBox3.Name = "groupBox3";
  122. this.groupBox3.Size = new System.Drawing.Size(340, 48);
  123. this.groupBox3.TabIndex = 6;
  124. this.groupBox3.TabStop = false;
  125. this.groupBox3.Text = "groupBox3";
  126. //
  127. // Box1
  128. //
  129. this.Box1.Location = new System.Drawing.Point(280, 8);
  130. this.Box1.Name = "Box1";
  131. this.Box1.Size = new System.Drawing.Size(72, 56);
  132. this.Box1.TabIndex = 7;
  133. this.Box1.TabStop = false;
  134. //
  135. // Form1
  136. //
  137. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  138. this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
  139. this.ClientSize = new System.Drawing.Size(360, 321);
  140. this.Controls.Add(this.Box1);
  141. this.Controls.Add(this.bsend);
  142. this.Controls.Add(this.tmsg);
  143. this.Controls.Add(this.groupBox2);
  144. this.Controls.Add(this.groupBox3);
  145. this.MaximumSize = new System.Drawing.Size(376, 360);
  146. this.MinimumSize = new System.Drawing.Size(376, 360);
  147. this.Name = "Form1";
  148. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  149. this.Text = "Server";
  150. this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
  151. this.Load += new System.EventHandler(this.Form1_Load_1);
  152. this.groupBox2.ResumeLayout(false);
  153. ((System.ComponentModel.ISupportInitialize)(this.Box1)).EndInit();
  154. this.ResumeLayout(false);
  155. this.PerformLayout();
  156.  
  157. }
  158. #endregion
  159.  
  160. /// <summary>
  161. /// The main entry point for the application.
  162. /// </summary>
  163. [STAThread]
  164. static void Main()
  165. {
  166. Application.Run(new Form1());
  167. }
  168. //----------------------------------------------------------
  169. /* this function run server and waiting for
  170. * a connection from the client
  171. * then exchange data between them
  172. */
  173. public void RunServer()
  174. {
  175. this.Box1.Image=Image.FromFile("2.jpg");
  176. try
  177. {
  178. //step 1: create TcpListener
  179. listener=new TcpListener(20000);
  180. // Step 2: TcpListener waits for connection request
  181. listener.Start();
  182. // Step 3: establish connection upon client request
  183. while(true)
  184. {
  185. tchat.Text="Waiting for new connection";
  186. // accept an incoming connection
  187. client=listener.AcceptSocket();
  188. // create NetworkStream object associated with socket
  189. stream=new NetworkStream(client);
  190. // create objects for transferring data across stream
  191. reader=new BinaryReader(stream);
  192. writer=new BinaryWriter(stream);
  193. // send client welcome msg
  194. writer.Write("Server :welcome to server");
  195. tchat.Text+="\r\n Server :welcome to server";
  196. string reply;
  197. // Step 4: read String data sent from client
  198. while(true)
  199. {
  200. try
  201. {
  202. //test if connection good
  203. if(client.Connected==false)
  204. {
  205. //client terminated
  206. break;
  207. }
  208. //Note : the function ReadString() will wait
  209. //until a message becomes available
  210. reply=reader.ReadString();
  211. /* to set the color to blue */
  212. int len=tchat.Text.Length;
  213. tchat.AppendText("\r\n Client :"+reply);
  214. tchat.Select(len,11+reply.Length);
  215. tchat.SelectionColor=Color.Blue;
  216. tchat.ScrollToCaret();
  217. }
  218. // Step 5: close connection
  219. catch(Exception)
  220. {
  221. tchat.Text+="\r\n Client terminated";
  222. //close connection with the current client
  223. reader.Close();
  224. writer.Close();
  225. stream.Close();
  226. client.Close();
  227.  
  228. break;
  229. }//end catch of the try that read string
  230. }//end while loop for reading
  231. }//end the while of the whole connection
  232. }//end of the try block of the whole connection
  233. catch(Exception)
  234. {
  235. MessageBox.Show(this,"Error connecting to client ",
  236. this.Text,MessageBoxButtons.OK,
  237. MessageBoxIcon.Error);
  238. }
  239. }
  240.  
  241. private void Form1_Load(object sender, System.EventArgs e)
  242. {
  243. //axSkin1.ApplySkin((int)Handle);
  244. }
  245.  
  246. private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  247. {
  248. //kill all resources of the server
  249. System.Environment.Exit( System.Environment.ExitCode );
  250. }
  251.  
  252. private void tmsg_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  253. {
  254. string text=tmsg.Text;
  255. if(e.KeyChar=='\r' && client!=null)
  256. {
  257. int len=tchat.Text.Length;
  258. tchat.AppendText("\r\n Server :"+tmsg.Text);
  259. tchat.Select(len,10+tmsg.Text.Length);
  260. tchat.SelectionColor=Color.Red;
  261. writer.Write(tmsg.Text);
  262. tmsg.Clear();
  263. tchat.ScrollToCaret();
  264. }
  265. }
  266.  
  267. private void bsend_Click(object sender, System.EventArgs e)
  268. {
  269. if( client!=null)
  270. {
  271. //int len=tchat.Text.Length;
  272. //tchat.AppendText("\r\n Server :"+tmsg.Text);
  273. //tchat.Select(len,10+tmsg.Text.Length);
  274. //tchat.SelectionColor=Color.Red;
  275. writer.Write(tmsg.Text);
  276. //tmsg.Clear();
  277. //tchat.ScrollToCaret();
  278. }
  279. }
  280.  
  281. private void Form1_Load_1(object sender, EventArgs e)
  282. {
  283.  
  284. }
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement