Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Threading;
- using System.Net.Sockets;
- using System.IO;
- //-----------------------------------------------------------
- //-----------------------------------------------------------
- namespace Server
- {
- /// <summary>
- /// Summary description for Form1.
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- //private AxACTIVESKINLib.AxSkin axSkin1;
- private System.Windows.Forms.TextBox tmsg;
- private System.Windows.Forms.Button bsend;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.GroupBox groupBox3;
- //--------------server connection variables ----------
- private Thread read;
- private NetworkStream stream;
- private BinaryReader reader;
- private BinaryWriter writer;
- private TcpListener listener;
- private Socket client;
- private System.Windows.Forms.PictureBox Box1;
- private System.Windows.Forms.RichTextBox tchat;
- //----------------------------------------------------
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- InitializeComponent();
- read=new Thread(new ThreadStart(RunServer));
- // read .s
- read.Start();
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.tmsg = new System.Windows.Forms.TextBox();
- this.bsend = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.tchat = new System.Windows.Forms.RichTextBox();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.Box1 = new System.Windows.Forms.PictureBox();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.Box1)).BeginInit();
- this.SuspendLayout();
- //
- // tmsg
- //
- this.tmsg.Location = new System.Drawing.Point(24, 256);
- this.tmsg.Name = "tmsg";
- this.tmsg.Size = new System.Drawing.Size(320, 20);
- this.tmsg.TabIndex = 0;
- this.tmsg.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tmsg_KeyPress);
- //
- // bsend
- //
- this.bsend.BackColor = System.Drawing.SystemColors.HighlightText;
- this.bsend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.bsend.Location = new System.Drawing.Point(152, 296);
- this.bsend.Name = "bsend";
- this.bsend.Size = new System.Drawing.Size(48, 24);
- this.bsend.TabIndex = 3;
- this.bsend.Text = "&Send";
- this.bsend.UseVisualStyleBackColor = false;
- this.bsend.Click += new System.EventHandler(this.bsend_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.tchat);
- this.groupBox2.Location = new System.Drawing.Point(12, 64);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(340, 160);
- this.groupBox2.TabIndex = 5;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "groupBox2";
- //
- // tchat
- //
- this.tchat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
- this.tchat.Location = new System.Drawing.Point(8, 24);
- this.tchat.Name = "tchat";
- this.tchat.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
- this.tchat.Size = new System.Drawing.Size(320, 120);
- this.tchat.TabIndex = 0;
- this.tchat.Text = "";
- //
- // groupBox3
- //
- this.groupBox3.Location = new System.Drawing.Point(12, 240);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(340, 48);
- this.groupBox3.TabIndex = 6;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "groupBox3";
- //
- // Box1
- //
- this.Box1.Location = new System.Drawing.Point(280, 8);
- this.Box1.Name = "Box1";
- this.Box1.Size = new System.Drawing.Size(72, 56);
- this.Box1.TabIndex = 7;
- this.Box1.TabStop = false;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
- this.ClientSize = new System.Drawing.Size(360, 321);
- this.Controls.Add(this.Box1);
- this.Controls.Add(this.bsend);
- this.Controls.Add(this.tmsg);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox3);
- this.MaximumSize = new System.Drawing.Size(376, 360);
- this.MinimumSize = new System.Drawing.Size(376, 360);
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Server";
- this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
- this.Load += new System.EventHandler(this.Form1_Load_1);
- this.groupBox2.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.Box1)).EndInit();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- //----------------------------------------------------------
- /* this function run server and waiting for
- * a connection from the client
- * then exchange data between them
- */
- public void RunServer()
- {
- this.Box1.Image=Image.FromFile("2.jpg");
- try
- {
- //step 1: create TcpListener
- listener=new TcpListener(20000);
- // Step 2: TcpListener waits for connection request
- listener.Start();
- // Step 3: establish connection upon client request
- while(true)
- {
- tchat.Text="Waiting for new connection";
- // accept an incoming connection
- client=listener.AcceptSocket();
- // create NetworkStream object associated with socket
- stream=new NetworkStream(client);
- // create objects for transferring data across stream
- reader=new BinaryReader(stream);
- writer=new BinaryWriter(stream);
- // send client welcome msg
- writer.Write("Server :welcome to server");
- tchat.Text+="\r\n Server :welcome to server";
- string reply;
- // Step 4: read String data sent from client
- while(true)
- {
- try
- {
- //test if connection good
- if(client.Connected==false)
- {
- //client terminated
- break;
- }
- //Note : the function ReadString() will wait
- //until a message becomes available
- reply=reader.ReadString();
- /* to set the color to blue */
- int len=tchat.Text.Length;
- tchat.AppendText("\r\n Client :"+reply);
- tchat.Select(len,11+reply.Length);
- tchat.SelectionColor=Color.Blue;
- tchat.ScrollToCaret();
- }
- // Step 5: close connection
- catch(Exception)
- {
- tchat.Text+="\r\n Client terminated";
- //close connection with the current client
- reader.Close();
- writer.Close();
- stream.Close();
- client.Close();
- break;
- }//end catch of the try that read string
- }//end while loop for reading
- }//end the while of the whole connection
- }//end of the try block of the whole connection
- catch(Exception)
- {
- MessageBox.Show(this,"Error connecting to client ",
- this.Text,MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- }
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- //axSkin1.ApplySkin((int)Handle);
- }
- private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- //kill all resources of the server
- System.Environment.Exit( System.Environment.ExitCode );
- }
- private void tmsg_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- string text=tmsg.Text;
- if(e.KeyChar=='\r' && client!=null)
- {
- int len=tchat.Text.Length;
- tchat.AppendText("\r\n Server :"+tmsg.Text);
- tchat.Select(len,10+tmsg.Text.Length);
- tchat.SelectionColor=Color.Red;
- writer.Write(tmsg.Text);
- tmsg.Clear();
- tchat.ScrollToCaret();
- }
- }
- private void bsend_Click(object sender, System.EventArgs e)
- {
- if( client!=null)
- {
- //int len=tchat.Text.Length;
- //tchat.AppendText("\r\n Server :"+tmsg.Text);
- //tchat.Select(len,10+tmsg.Text.Length);
- //tchat.SelectionColor=Color.Red;
- writer.Write(tmsg.Text);
- //tmsg.Clear();
- //tchat.ScrollToCaret();
- }
- }
- private void Form1_Load_1(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement