2011-01-29 3 views
1

안녕하세요. VB.NET 2008에서 제대로 작동하는 VB6에서 작동하는이 코드를 얻으려고합니다. 연결하고 싶지는 않지만 (예 : sockMain.Connect() 이제VB.net의 Winsock이 작동하지 않습니다.

sockMain.RemoteHost = "192.168.1.77" 
sockMain.RemotePort = 77 
sockMain.Connect() 

내가이 수행합니다...

On Error GoTo oops 
    sockMain.SendData(txtSend.Text) 

oops: 
    If Err.Number = 40006 Then 
     MsgBox("It doesnt seem that the server is running. Please check it and try again") 
    End If 

내가 얻을 실행하는 서버가 그것을 확인하고 다시 시도하십시오 보일 나던 그것은 오류

무엇이 부족합니까 ??

David

+0

을위한 acticle의 끝 부분에 주석을보고 싶은 경우 당신이 놓치고있는 첫 번째 것은 VB 6와 VB.NET이 외형 적으로 유사한 문법 만있는 * 완전히 다른 언어입니다. 즉, 구조화 된 예외 처리 ('try' /'catch')를 사용할 수 있기 때문에 VB.NET에서는'On Error GoTo'가 * more * 이상 사용되지 않습니다. 좋은 책을 얻고 .NET 관용구뿐만 아니라 일반적으로 객체 지향 프로그래밍을 배우십시오. VB6에서 VB.NET으로 다시 작성하면 실제로 가치가있을 것이기 때문에 장기적으로 큰 호의를 보일 것입니다. –

답변

4

VB.NET과 VB 6은 거의 완전히 다른 프로그래밍 언어입니다. 당신은 VB.NET에서 VB 6 코드를 작성하려고 시도하여 어떤 호의도하지 않습니다. .NET 플랫폼에서 제공하는 새로운 기능을 이용하지 않으려는 경우 마이그레이션 할 이유가 없습니다.

앞서 언급 한 구조적 예외 처리 이외에도 이전 WinSock 컨트롤을 사용하여 System.Net.Sockets namespace에있는 클래스를 사용하여 제거해야합니다. 다음과 같은 코드로 무엇을 교체

시도 :

Dim tcpClient As New System.Net.Sockets.TcpClient() 
tcpClient.Connect("192.168.1.77", 77) 
Dim networkStream As NetworkStream = tcpClient.GetStream() 
If networkStream.CanWrite And networkStream.CanRead Then 
    ' Do a simple write. 
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there") 
    networkStream.Write(sendBytes, 0, sendBytes.Length) 

    ' Read the NetworkStream into a byte buffer. 
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte 
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) 

    ' Output the data received from the host to the console. 
    Dim returnData As String = Encoding.ASCII.GetString(bytes) 
    Console.WriteLine(("Host returned: " + returnData)) 
Else 
    If Not networkStream.CanRead Then 
     Console.WriteLine("Cannot not write data to this stream. " & 
          "Please check the server and try again.") 
     tcpClient.Close() 
    Else 
     If Not networkStream.CanWrite Then 
      Console.WriteLine("Cannot read data from this stream. " & 
           "Please check the server and try again.") 
      tcpClient.Close() 
     End If 
    End If 
End If 
0

Connect (연결) 호출을 완료하는 데 약간의 시간이 걸릴 수 있습니다. 클라이언트가 서버에 물리적으로 연결 한 경우에도 TCP 가상 회선을 설정하는 동안이를 제공해야합니다. SendData에 대한 호출에 중단 점을 넣고 잠깐 기다린 다음 계속하면 아마도 정상적으로 작동하는 것입니다.

1

당신은 그물 세계에서 VB6의 윈속의 느낌, this 시도는 2008 년 이후 업데이트되지 않은 몇 버그가 조심 자세한 내용