게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
비주얼베이직.net 독학자좀 도와주세요ㅠㅠ
게시물ID : programmer_14130짧은주소 복사하기
작성자 : 공대문순이
추천 : 0
조회수 : 522회
댓글수 : 1개
등록시간 : 2015/10/28 02:53:41
옵션
  • 본인삭제금지
채팅 프로그램을 유튜브 보면서 이것저것 해보는데 계속 오류가 뜨네요 혹시 이 오류가 무슨 의미인지 아시는 분 계시나요??ㅜㅜ 책에도 없고 인터넷에도 없고ㅜㅜ
 
서버와 클라이언트를 두개로 나누고 아래 창은 클라이언트를 디버깅하고 서버에 접속하려니까 뜨는 에러입니다ㅠㅠ 혹시 몰라 서버와 클라이언트 소스도 올리겠습니다.
 
<클라이언트>
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream
    Dim readData As String
    Dim infiCount As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub
    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
        Else
            TextBox2.Text = TextBox2.Text + Environment.NewLine + " >> " + readData
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        readData = "Connect to Chat server"
        msg()
        clientSocket.Connect("127.0.0.1", 8888)
        serverStream = clientSocket.GetStream()
        Dim outstream As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text + "$")
        serverStream.Write(outstream, 0, outstream.Length)
        serverStream.Flush()
        Dim CtThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
        CtThread.Start()
    End Sub
    Private Sub getMessage()
        For infiCount = 1 To 2
            infiCount = 1
            serverStream = clientSocket.GetStream()
            Dim bsize As Integer
            Dim instream(10024) As Byte
            bsize = clientSocket.ReceiveBufferSize
            serverStream.Read(instream, 0, bsize)
            Dim returndata As String = System.Text.Encoding.ASCII.GetString(instream)
            returndata = "" + returndata
            msg()

        Next
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
    End Sub
End Class
 
//밑줄부분이 계속 오류 나는 곳입니다..ㅜㅜㅜ
 
<서버>
Imports System.Net.Sockets
Imports System.Text
Module Module1
    Dim clientlist As Hashtable

    Sub Main()
        Dim ServerSocket As New TcpListener(8888)
        Dim clientsocket As TcpClient
        Dim count As Integer
        ServerSocket.Start()
        Msg("Chat server started")
        count = 0
        While (True)
            count += 1
            clientsocket = ServerSocket.AcceptTcpClient()
            Dim byteFrom(10024) As Byte
            Dim DataFromClient As String
            Dim NS As NetworkStream = clientsocket.GetStream()
            NS.Read(byteFrom, 0, CInt(clientsocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(byteFrom)
            dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
            clientlist(DataFromClient) = clientsocket
            BroadCast(DataFromClient + "Joined", DataFromClient, False)
            Msg(DataFromClient + "Joined chat room")
            Dim c As New clientFous
            c.ClientStrart(clientsocket, DataFromClient, clientlist)
        End While
        clientsocket.Close()
        ServerSocket.Stop()
        Msg("Exit")
        Console.ReadLine()
    End Sub
    Sub Msg(ByVal msg As String)
        msg.Trim()
        Console.WriteLine(" >> " + msg)
    End Sub
    Private Sub BroadCast(ByVal msg As String, ByVal Uname As String, ByVal flag As Boolean)
        Dim item As DictionaryEntry
        For Each item In clientlist
            Dim broadcastsocket As TcpClient
            broadcastsocket = CType(item.Value, TcpClient)
            Dim bcs As NetworkStream = broadcastsocket.GetStream()
            Dim Bcb As [Byte]()
            If flag = True Then
                Bcb = Encoding.ASCII.GetBytes(Uname + " says: " + msg)
            Else
                Bcb = Encoding.ASCII.GetBytes(msg)
            End If
            bcs.Write(Bcb, 0, Bcb.Length)
            bcs.Flush()
        Next
    End Sub
    Public Class clientFous
        Dim clientsocket As TcpClient
        Dim clno As String
        Dim clientlist As Hashtable
        Public Sub ClientStrart(ByVal inclientsocket As TcpClient, ByVal clientno As String, ByVal clist As Hashtable)
            Me.clientsocket = inclientsocket
            Me.clno = clientno
            Me.clientlist = clist
            Dim CTTread As Threading.Thread = New Threading.Thread(AddressOf ichat)
            CTTread.Start()
        End Sub
        Private Sub ichat()
            Dim requestcount As Integer
            Dim bytefrom(10024) As Byte
            Dim datafromclient As String
            Dim sendbyte As [Byte]()
            Dim serverResponse As String
            Dim Rcount As String
            requestcount = 0
            While (True)
                Try
                    requestcount += 1
                    Dim NS As NetworkStream = clientsocket.GetStream()
                    NS.Read(bytefrom, 0, CInt(clientsocket.ReceiveBufferSize))
                    datafromclient = System.Text.Encoding.ASCII.GetString(bytefrom)
                    datafromclient = datafromclient.Substring(0, datafromclient.IndexOf("$"))
                    Msg("From client - " + clno + " : " + datafromclient)
                    Rcount = Convert.ToString(requestcount)
                    BroadCast(datafromclient, clno, True)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
        End Sub
    End Class
End Module
1.png
2.png
윈도우.jpg
3.png
 
 
 
 
질문이 너무 터무니 없이 막연해 보이기도 하지만ㅠㅠ 조금의 도움을 주신다면 미래의 개발자가 좀 더 배워가는 큰 기회가 될것같습니다. 죄송합니다ㅠㅠ
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호