26. august 2003 - 11:49
Der er
11 kommentarer og 1 løsning
Virker i konsolprogram men ikke i grafisk!
Hej. Hvorfor virker denne linje ikke i mit grafiske program når det virker i mit konsol program? Socket s = myList.AcceptSocket(); Den melder ikke nogen fejl men hænger kun? (jeg bruger VS.NET)
Annonceindlæg fra HP
26. august 2003 - 12:32
#1
Linien er nok blokerende. Når den exsekverer den, låser den til der kommer en connection og imens er din brugerflade låst (med mindre du kører asynckron eller i tråde)
26. august 2003 - 12:35
#2
chries> Du skulle vel ikke have MSN? Jeg har flg.: private void Form1_Load(object sender, System.EventArgs e) { try { IPAddress ipAd = IPAddress.Parse("192.168.2.103"); //use local m/c IP address, and use the same in the client TcpListener myList = new TcpListener(ipAd ,20); myList.Start(); txtLog.Text = "Server er startet på flg. IP: " + myList.LocalEndpoint.ToString(); Socket s = myList.AcceptSocket(); myList.Stop(); } catch (Exception e1) { MessageBox.Show(e1.StackTrace); } }
26. august 2003 - 12:48
#3
Jeg har lige testet dit kode i VS .NET. Det er fordi den venter på en connection. (Socket s = myList.AcceptSocket()). Du er nødtil at køre asynckron sockets eller tråde for at undgå din brugeflade "låser".
26. august 2003 - 12:51
#4
26. august 2003 - 12:55
#5
Har du ikke MSN eller ICQ jeg lige kan kontakte dig på? //>Rune
26. august 2003 - 12:56
#6
-Det var til chries! //>Rune
26. august 2003 - 13:01
#7
icq 16722008, men jeg er til møde den næste time
26. august 2003 - 14:57
#8
Husk nu at komme med svaret her også, og ikke bare på ICQ... Men det er korrekt at programmet ser ud til at hænge, da det står og venter på en connection - så med mindre programmet godt må hænge, ville det nok være en idé med nogle tråde...
26. august 2003 - 14:59
#9
Min kolega (System32) og mig har fået denne briliante løsning af chries: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace NetIT_2_Day { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private const int port = 8090; private Thread serverThread = null; private TcpListener tcpListener = null; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.ToolBar toolBar1; private System.Windows.Forms.ToolBarButton toolBarButton1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox2; private System.Windows.Forms.ListBox listBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <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() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.toolBar1 = new System.Windows.Forms.ToolBar(); this.toolBarButton1 = new System.Windows.Forms.ToolBarButton(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox2 = new System.Windows.Forms.PictureBox(); this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem3}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2}); this.menuItem1.Text = "Filer"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "Luk"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem4}); this.menuItem3.Text = "Hjælp"; // // menuItem4 // this.menuItem4.Index = 0; this.menuItem4.Text = "Dokumentation"; // // toolBar1 // this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.toolBarButton1}); this.toolBar1.DropDownArrows = true; this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = true; this.toolBar1.Size = new System.Drawing.Size(618, 39); this.toolBar1.TabIndex = 0; this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick); // // toolBarButton1 // this.toolBarButton1.Text = "Start server"; this.toolBarButton1.ToolTipText = "Start server"; // // pictureBox1 // this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(24, 8); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(16, 16); this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; // // pictureBox2 // this.pictureBox2.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox2.Image"))); this.pictureBox2.Location = new System.Drawing.Point(24, 8); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(16, 16); this.pictureBox2.TabIndex = 2; this.pictureBox2.TabStop = false; // // listBox1 // this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.listBox1.Location = new System.Drawing.Point(0, 39); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(618, 355); this.listBox1.TabIndex = 3; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(618, 398); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listBox1, this.pictureBox2, this.pictureBox1, this.toolBar1}); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "NetIT 2 Day"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { } private void menuItem2_Click(object sender, System.EventArgs e) { this.Close(); } private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { if (this.toolBarButton1.Text == "Start server") { this.toolBarButton1.Text = "Stop server"; this.pictureBox1.Visible = true; this.pictureBox2.Visible = false; listBox1.Items.Add("Server er startet."); serverThread = new Thread(new ThreadStart(StartListen)); serverThread.Start(); } else { listBox1.Items.Add("Server er stoppet."); tcpListener.Stop(); serverThread.Interrupt(); this.toolBarButton1.Text = "Start server"; this.pictureBox1.Visible = false; this.pictureBox2.Visible = true; } } public void StartListen() { try { tcpListener = new TcpListener(port); tcpListener.Start(); while(true) { listBox1.Items.Add("Server venter på klienter."); Socket soTcp = tcpListener.AcceptSocket(); listBox1.Items.Add("Klient accepteret."); NetworkStream networkStream = new NetworkStream(soTcp); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); streamWriter.AutoFlush = true; streamWriter.WriteLine( "C#: jeg ruller! Farvel og tak" ); listBox1.Items.Add("Klient behandlet."); soTcp.Close(); } } catch( Exception e ) { // tread aborted //MessageBox.Show( e.ToString() ); } } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if( serverThread != null ) { serverThread.Interrupt(); } if( tcpListener != null ) { tcpListener.Stop(); } } } } Så chries> Læg endelig et svar og du skal få points! //>Rune
26. august 2003 - 15:00
#10
Svar :-)
26. august 2003 - 15:04
#11
Anders (System32) er desvære taget hjem fra i dag, men han acceptere lige dit svar i morgen... //>Rune
27. august 2003 - 08:59
#12
Mange tak for hjælpen chries! Mvh. Anders.
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.