-
January 6th, 2013, 07:54 AM
#1
form do not take any action when tcpListener is start
hi
i create a very simple server-client chat program.
in the server form , i put a textbox to recieve msg from clients and a button to start tcpListener.when i click this button tcpListener is start but the form is freeze.i cant choose any thing in this form.what can i do to fix this problem?
-
January 6th, 2013, 01:24 PM
#2
Re: form do not take any action when tcpListener is start
Yoy will have to show some code, as the problem can be anything, and without seeing your code; it is way too difficult to exactly tell you where the problems are.
-
January 6th, 2013, 02:57 PM
#3
Re: form do not take any action when tcpListener is start
sure ...
this is the server codes :
Code:
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Public Class Server
Public ip As IPAddress = GetMyIP()
Public TCPlistener As TcpListener = New TcpListener(ip, 2500)
Private Sub Chat()
Dim Messages(7) As String
Dim a As New AutoResetEvent(False)
While CheckBox1.Checked = True
Try
TCPlistener.Start()
ListBox1.Items.Clear()
ListBox1.Items.Add("The server is ruuning as port :" & Str(2500))
Dim F As New Form
Controls.Add(F)
F.Size = New Size(100, 100)
F.Refresh()
Dim MyEndpoint As IPEndPoint = CType(TCPlistener.LocalEndpoint, IPEndPoint)
ListBox1.Items.Add("The local EndPoint address is :" & MyEndpoint.Address.ToString)
ListBox1.Items.Add("Waiting for connectiom ...")
Dim sock As Socket = TCPlistener.AcceptSocket
MyEndpoint = CType(sock.RemoteEndPoint, IPEndPoint)
ListBox1.Items.Add("Connection accepted from :" & MyEndpoint.Address.ToString)
Dim ByteData(100) As Byte
Dim size As Integer = sock.Receive(ByteData)
ListBox1.Items.Add("Recieved")
Dim RecieveText As String = Nothing
For i = 0 To size
RecieveText = RecieveText & Convert.ToChar(ByteData(i))
Next
Dim AsciiEncod As New ASCIIEncoding
ListBox1.Items.Add("Sending alert to client !")
sock.Send(AsciiEncod.GetBytes("The string was recieved by the server !"))
sock.Close()
MsgBox(RecieveText)
TCPlistener.Stop()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End While
End Sub
End Class
this is the client codes :
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Public Class Form1
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
txtMessages.Text = Nothing
Dim TCPclient As New TcpClient
txtMessages.Text = txtMessages.Text & "Connecting ..." & vbCrLf
Dim MyIP As IPAddress = GetMyIP()
Dim SysPort As Int32 = Convert.ToInt16(txtPort.Text)
TCPclient.Connect(MyIP.ToString, SysPort)
txtMessages.Text = txtMessages.Text & "Connected" & vbCrLf
txtMessages.Text = txtMessages.Text & "Enter a text to send ..." & vbCrLf
txtSendMess.Focus()
Dim StrMessage As String = txtSendMess.Text
Dim stm As Stream = TCPclient.GetStream
Dim AsciiEncod As New ASCIIEncoding
Dim ByteData() As Byte = AsciiEncod.GetBytes(StrMessage)
txtMessages.Text = txtMessages.Text & "Sending" & vbCrLf
stm.Write(ByteData, 0, ByteData.Length)
Dim ReplyMessage(100) As Byte
Dim size As Integer = stm.Read(ReplyMessage, 0, 100)
txtMessages.Text = txtMessages.Text & "This msg is from server :"
Dim str As String = Nothing
For i = 0 To size
str = str & Convert.ToChar(ReplyMessage(i))
Next
txtMessages.Text = txtMessages.Text & str & vbCrLf
TCPclient.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
i searched in google and find something about threading . if it is possible for you show me the solution with code or with correct this code .
-
January 7th, 2013, 12:07 AM
#4
Re: form do not take any action when tcpListener is start
Look at working examples for your language choice. http://code.msdn.microsoft.com/
That will explain the problems you are having.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|