请帮忙,套接字/线程 [英] Please help, Sockets / Threading

查看:58
本文介绍了请帮忙,套接字/线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone


我正在制作即时通讯程序。我已经使用MSDN套接字示例来开始这个。我已经将在这个项目中找到的代码转移到一个类中并且可以很好地访问它。

问题是,信使向服务器发送消息,该消息将消息转发给正确的用户:很棒,但有一个线程正在运行,正在读取输入数据并将其传递给进程子,该子进程检查服务器已发送的内容


现在我正在尝试获取一个表单的新实例,以便在收到消息时加载,但是当它运行时表单加载但只是滞后并且没有正确加载,就像它试图一遍又一遍地做同样的事情


很奇怪,因为数据被发送到服务器 - >服务器发送给客户端 - >客户端打开一个新窗口(这个滞后),但只有一大堆数据,它只是一直在做,就像它一遍又一遍地做。我怎样才能让它工作


如果您需要一些代码示例,请告诉我我的msn ma**@xoolo.net (这不是我的电子邮件只是MSN)


感谢任何可以提供帮助的人。

Hello Everyone

I am making an instant messenger program. I have used the MSDN sockets example to get started with this. I have transfered the code that is found within this project into a class and can access it fine

The problem is, that the messenger sends a message to the server which relays the message to the correct user: great, but there is a thread running that is reading the incomming data and passes it to a process sub that checks to see what the server has sent

Now i am trying to get a new instance of a form to load up when a message is received but when it runs the form loads up but just lags and doesnt load properly, its like its trying to do the same thing over and over again

Its weird because data gets sent to the server - > server sends to the client - > client opens a new window ( this lags ), but there is only one lot of data and it just keeps lagging like it is doing it over and over and over. How can i get this to work

If you need some code examples, just get me on my msn which is ma**@xoolo.net ( THIS IS NOT MY EMAIL JUST MSN )

Thanks to anyone that can help.

推荐答案

Matt写道:
大家好,

我正在制作即时通讯程序。我已经使用MSDN
套接字示例来开始这个。我已经将在这个项目中找到的代码转移到了一个类中并且可以访问它
很好。

问题是,messenger向服务器发送消息
将消息转发给正确的用户:很好,但是有一个正在运行的线程正在读取输入数据并将其传递给
进程子,该子进程检查服务器发送了什么。

现在我正在尝试获取一个表单的新实例,以便在收到
消息时加载但是当它运行时表单加载但只是滞后
并且不会正确加载,就像它试图一遍又一遍地做同样的事情。
Hello Everyone,

I am making an instant messenger program. I have used the MSDN
sockets example to get started with this. I have transfered the code
that is found within this project into a class and can access it
fine.

The problem is, that the messenger sends a message to the server
which relays the message to the correct user: great, but there is a
thread running that is reading the incomming data and passes it to a
process sub that checks to see what the server has sent.

Now i am trying to get a new instance of a form to load up when a
message is received but when it runs the form loads up but just lags
and doesnt load properly, its like its trying to do the same thing
over and over again.




听起来你正在尝试创建新的表单处理传入数据的

线程的上下文。此线程通常会阻止

,直到新数据进入,从而阻止您的表单(在该b / b
线程中运行)执行它的代码。


你想要做的是在同一个线程中创建所有的UI。创建一个

函数来处理''创建新表单''位。为这个

函数的签名创建一个委托,或者让签名匹配EventHandler或

MethodInvoker委托,这是最快的。现在使用

主窗体上的Invoke方法在UI线程的上下文中运行此函数。


这应该照顾它,除非我误解了你的意图。


-

Sven Groot



It sounds like you are trying to create the new form in the context of the
thread that handles the incoming data. This thread will typically block
until new data comes in, preventing your form, which is running in that
thread, from executing it''s code.

What you want to do is create all your UI in the same threads. Create a
function that handles the ''create new form'' bit. Create a delegate for this
function''s signature, or have the signature match either the EventHandler or
MethodInvoker delegate, that''s fastest. Now use the Invoke method on your
main form to run this function in your UI thread''s context.

That should take care of it, unless I misunderstand your intentions.

--
Sven Groot


Hi Matt


我也参与过类似的项目。我想你需要改变你的拱门......我会在线汤姆...在我的办公室msn被阻止了。汤姆......我会在家里和你聊天。我的MSN ID是 sa ************ @ hotmail.com


Sadha Sivam

Malleable Minds Software Pvt Ltd
Hi Matt

I have also worked on similar project. I think you need to change your arch.... I will online tom... In my office msn is blocked. Tom... I will chat with you from my home. My MSN ID is sa************@hotmail.com.

Sadha Sivam
Malleable Minds Software Pvt Ltd


这就是Sven发生的事情Groot,我是一个新的vb.net开发者

从vb6改变了什么嘿嘿。


你能不能给我一个快速的例子来说明我的需要。


以下是代码发生的地方:


此部分已填写表格负载

----------------------------------------

Public Sub tryConnect()

尝试

''TcpClient是Socket的子类,提供更高的

级别

' '像流一样的功能。

client =新的TcpClient(strServer,PORT_NUM)


''启动异步读取调用DoRead以避免

落后于用户

''界面。

client.GetStream.BeginRead(readBuffer,0,READ_BUFFER_SIZ E,

AddressOf DoRead,Nothing)


赶上Ex Exception

结束

结束尝试

如果strUsername> ""然后

''当用户第一次进入程序时

''连接命令被发送到服务器

SendData(" CNT" &" |"& strUsername&" |"& strNickName)

结束如果

结束子

- ----------------------------------------

本部分是下一个

---------------------------------------- -

Public Sub DoRead(ByVal ar As IAsyncResult)

Dim BytesRead As Integer

Dim strMessage As String


尝试

''完成异步读入readBuffer并返回数字

读取的字节数。

BytesRead = client.GetStream.EndRead (ar)

如果BytesRead< 1然后

''如果没有读取字节,则服务器已关闭。禁用输入

窗口。

退出Sub

结束如果

''转换字节数组消息已保存进入,减去

两个用于

''Chr(13)和Chr(10)

strMessage = Encoding.ASCII.GetString(readBuffer ,0,

BytesRead - 2)


----------------------- ------------------

本子的这一部分是在哪里检查命令,通过另一个

SUB

-----------------------------------------

ProcessCommands(strMessage)


''开始一个新的异步读入readBuffer。

client.GetStream.BeginRead(readBuffer,0 ,READ_BUFFER_SIZE,

AddressOf DoRead,Nothing)

Catch e As Exception

MsgBox(e.Message)

结束尝试


结束子

-------------------------- ---------------

以下子程序命令

------------- ----------------------------

Public Sub ProcessCommands(ByVal strMessage As String)

''TMSG | 1234567890 | Matt | 1234567890 | Message

Dim dataArray()As String

Dim lstR作为ListViewItem

dataArray = Split(strMessage,Chr(124))

Select Case dataArray(0)

Case" RNICK"

''lstR = lstContacts.Items.Add(dataArray(2))

''lstR.SubItems.Add(dataArray(1))

-----------------------------------------

这就是问题区域,理想的是它有足够的好处

从线上运行所以我可以在这里执行代码

---- -------------------------------------

Case" TMSG"

''打开消息窗口

''显示收到的消息

''打开一个新窗口,或打开带有标签的窗口

如果F4什么都没有那么

F4 =新的frmMessage

F4.Show()

结束如果

Case" ERROR"
MsgBox (dataArray(1),MsgBoxStyle.Exclamation,错误

已发布)

结束选择

结束子

-----------------------------------------


我会给你提供更多的帮助。


亲切的问候


马特


***通过开发人员指南 http://www.developersdex.com ***

不要只参加USENET ......获得奖励!
Thats exactly what is happening Sven Groot, i am a new vb.net developer
what a change from vb6 hehe.

Anychance you could give me a quick example of what i have to do.

Here is where the code is happening:

THIS PART IS DONE ON THE FORM LOAD
----------------------------------------
Public Sub tryConnect()
Try
'' The TcpClient is a subclass of Socket, providing higher
level
'' functionality like streaming.
client = New TcpClient(strServer, PORT_NUM)

'' Start an asynchronous read invoking DoRead to avoid
lagging the user
'' interface.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf DoRead, Nothing)

Catch Ex As Exception
End
End Try
If strUsername > "" Then
''When the user first enters the program
''The connect command is sent to the server
SendData("CNT" & "|" & strUsername & "|" & strNickName)
End If
End Sub
-----------------------------------------
THIS PART IS NEXT
-----------------------------------------
Public Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String

Try
'' Finish asynchronous read into readBuffer and return number
of bytes read.
BytesRead = client.GetStream.EndRead(ar)
If BytesRead < 1 Then
'' If no bytes were read server has close. Disable input
window.
Exit Sub
End If
'' Convert the byte array the message was saved into, minus
two for the
''Chr(13) and Chr(10)
strMessage = Encoding.ASCII.GetString(readBuffer, 0,
BytesRead - 2)

-----------------------------------------
THIS PART OF THIS SUB IS WHERE IT CHECKS THE COMMANDS, PASSES TO ANOTHER
SUB
-----------------------------------------
ProcessCommands(strMessage)

'' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf DoRead, Nothing)
Catch e As Exception
MsgBox(e.Message)
End Try

End Sub
-----------------------------------------
THE FOLLOWING SUB PROCESSES THE COMMANDS
-----------------------------------------
Public Sub ProcessCommands(ByVal strMessage As String)
''TMSG|1234567890|Matt|1234567890|Message
Dim dataArray() As String
Dim lstR As ListViewItem
dataArray = Split(strMessage, Chr(124))
Select Case dataArray(0)
Case "RNICK"
''lstR = lstContacts.Items.Add(dataArray(2))
''lstR.SubItems.Add(dataArray(1))
-----------------------------------------
THIS IS THE PROBLEM AREA, IDEALLY IT WOULD BE GOOD TO HAVE THIS WHOLE
SUB RUNNING OUT OF THE THREAD SO I CAN EXECUTE CODE HERE
-----------------------------------------
Case "TMSG"
''open message window
''display received message
''Open a new window, or open window with tabs
If F4 Is Nothing Then
F4 = New frmMessage
F4.Show()
End If
Case "ERROR"
MsgBox(dataArray(1), MsgBoxStyle.Exclamation, "An Error
Has Occured")
End Select
End Sub
-----------------------------------------

I would apprechiate any further help you can give me.

Kind Regards

Matt

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于请帮忙,套接字/线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆