Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - .NET

Страница: 1 |

 

  Вопрос: Русские буквы - 2. Сервер наносит ответный удар Добавлено: 26.09.10 08:09  

Автор вопроса:  Ishayahu | Web-сайт: ishayahu.blogspot.com | ICQ: 329944992 
есть программа для работы через сеть . при посылке английского текста все работает замечательно, но при пересылке русского с клиента на сервер приходят одни лишь вопросы. как это можно исправить?
код сервера:
    Const PORT_NUM As Integer = 10000
    Dim strprint As String

    Private clients As New Hashtable()
    Private listener As TcpListener
    Private listenerThread As Threading.Thread

    ' This subroutine sends a message to all attached clients
    Private Sub Broadcast(ByVal strMessage As String)
        Dim client As UserConnection
        Dim entry As DictionaryEntry

        ' All entries in the clients Hashtable are UserConnection so it is possible
        ' to assign it safely.
        For Each entry In clients
            client = CType(entry.Value, UserConnection)
            client.SendData(strMessage)
        Next
    End Sub

    ' This subroutine sends the contents of the Broadcast textbox to all clients, if
    ' it is not empty, and clears the textbox
    Private Sub btnBroadcast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBroadcast.Click
        If txtBroadcast.Text <> "" Then
            UpdateStatus("Broadcasting: " & txtBroadcast.Text)
            Broadcast("BROAD|" & txtBroadcast.Text)

            txtBroadcast.Text = ""
        End If
    End Sub

    ' This subroutine checks to see if username already exists in the clients
    ' Hashtable.  If it does, send a REFUSE message, otherwise confirm with a JOIN.
    Private Sub ConnectUser(ByVal userName As String, ByVal sender As UserConnection)
        If clients.Contains(userName) Then
            ReplyToSender("REFUSE", sender)
        Else
            sender.Name = userName
            UpdateStatus(userName & " has joined the chat.")
            clients.Add(userName, sender)

            ' Send a JOIN to sender, and notify all other clients that sender joined
            ReplyToSender("JOIN", sender)
            SendToClients("CHAT|" & sender.Name & " has joined the chat.", sender)
        End If
    End Sub

    ' This subroutine notifies other clients that sender left the chat, and removes
    ' the name from the clients Hashtable
    Private Sub DisconnectUser(ByVal sender As UserConnection)
        UpdateStatus(sender.Name & " has left the chat.")
        SendToClients("CHAT|" & sender.Name & " has left the chat.", sender)
        clients.Remove(sender.Name)
    End Sub

    ' This subroutine is used as a background listener thread to allow reading incoming
    ' messages without lagging the user interface.
    Private Sub DoListen()
        Try
            ' Listen for new connections.
            listener = New TcpListener(System.Net.IPAddress.Any, PORT_NUM)
            listener.Start()
            Do
                ' Create a new user connection using TcpClient returned by
                ' TcpListener.AcceptTcpClient()
                Dim client As New UserConnection(listener.AcceptTcpClient)

                ' Create an event handler to allow the UserConnection to communicate
                ' with the window.
                AddHandler client.LineReceived, AddressOf OnLineReceived
                UpdateStatus("New connection found: waiting for log-in")
            Loop Until False
        Catch
        End Try
    End Sub

    ' When the window closes, stop the listener.
    Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        listener.Stop()
    End Sub

    ' Start the background listener thread.
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listenerThread = New Threading.Thread(AddressOf DoListen)
        listenerThread.Start()
        UpdateStatus("Listener started")
        listtemp.Hide()
        txtstatus.Hide()
    End Sub

    ' Concatenate all the client names and send them to the user who requested user list
    Private Sub ListUsers(ByVal sender As UserConnection)
        Dim client As UserConnection
        Dim entry As DictionaryEntry
        Dim strUserList As String

        UpdateStatus("Sending " & sender.Name & " a list of users online.")

        strUserList = "LISTUSERS"

        ' All entries in the clients Hashtable are UserConnection so it is possible
        ' to assign it safely.
        For Each entry In clients
            client = CType(entry.Value, UserConnection)
            strUserList = strUserList & "|" & client.Name
        Next

        ' Send the list to the sender.
        ReplyToSender(strUserList, sender)
    End Sub

    ' This is the event handler for the UserConnection when it receives a full line.
    ' Parse the cammand and parameters and take appropriate action.
    Private Sub OnLineReceived(ByVal sender As UserConnection, ByVal data As String)
        Dim dataArray() As String

        ' Message parts are divided by "|"  Break the string into an array accordingly.
        dataArray = data.Split(Chr(124))

        ' dataArray(0) is the command.
        Select Case dataArray(0)
            Case "CONNECT"
                ConnectUser(dataArray(1), sender)
            Case "CHAT"
                SendChat(dataArray(1), sender)
            Case "DISCONNECT"
                DisconnectUser(sender)
            Case "REQUESTUSERS"
                ListUsers(sender)
            Case "SRV"
                UpdateStatus("|" & dataArray(1))
            Case Else
                UpdateStatus("Unknown message:" & data)
        End Select
    End Sub

    ' This subroutine sends a response to the sender.
    Private Sub ReplyToSender(ByVal strMessage As String, ByVal sender As UserConnection)
        sender.SendData(strMessage)
    End Sub

    ' Send a chat message to all clients except sender.
    Private Sub SendChat(ByVal message As String, ByVal sender As UserConnection)
        UpdateStatus(sender.Name & ": " & message)
        SendToClients("CHAT|" & sender.Name & ": " & message, sender)
    End Sub

    ' This subroutine sends a message to all attached clients except the sender.
    Private Sub SendToClients(ByVal strMessage As String, ByVal sender As UserConnection)
        Dim client As UserConnection
        Dim entry As DictionaryEntry

        ' All entries in the clients Hashtable are UserConnection so it is possible
        ' to assign it safely.
        For Each entry In clients
            client = CType(entry.Value, UserConnection)

            ' Exclude the sender.
            If client.Name <> sender.Name Then
                client.SendData(strMessage)
            End If
        Next
    End Sub

 

End Class


код клиента в этой теме:
http://vbnet.ru/forum/show.aspx?id=209533&page=1

спасибо за помощь!

Ответить

  Ответы Всего ответов: 5  

Номер ответа: 1
Автор ответа:
 UnDeAdZak



Вопросов: 80
Ответов: 476
 Профиль | | #1 Добавлено: 26.09.10 13:07
Кодировки измени.

Ответить

Номер ответа: 2
Автор ответа:
 Ishayahu



ICQ: 329944992 

Вопросов: 4
Ответов: 21
 Web-сайт: ishayahu.blogspot.com
 Профиль | | #2
Добавлено: 26.09.10 13:39
а где они? я ни acii ни Encoding не вижу((

Ответить

Номер ответа: 3
Автор ответа:
 Artyom



Разработчик

Вопросов: 130
Ответов: 6602
 Профиль | | #3 Добавлено: 26.09.10 14:23
Я тебе в твоей теме ответил уже.

Ответить

Номер ответа: 4
Автор ответа:
 



Администратор

ICQ: 278109632 

Вопросов: 42
Ответов: 3949
 Web-сайт: domkratt.com
 Профиль | | #4
Добавлено: 26.09.10 16:39
Прошли времена Winsock.ocx и BASE64...

Ответить

Номер ответа: 5
Автор ответа:
 Ishayahu



ICQ: 329944992 

Вопросов: 4
Ответов: 21
 Web-сайт: ishayahu.blogspot.com
 Профиль | | #5
Добавлено: 26.09.10 17:02
Executionerто есть?

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам