2013-01-18 2 views
1
**Server Code** 

Dim serverSocket As Socket 
Dim clientSocket As Socket 
Dim PubIP as String = "82.XX.XX.XX" 
Dim LocalIP as String = "192.XX.XX.XX" 

Dim byteData(1023) As Byte 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim host As String = Dns.GetHostName 
    Dim ip As IPHostEntry = Dns.GetHostEntry(host) 
    serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 
    Dim IpEndPoint As IPEndPoint = New IPEndPoint(System.Net.IPAddress.Parse(PubIP), 8080) 
    Me.Text = IpEndPoint.ToString 
    serverSocket.Bind(IpEndPoint) 'it can't bind public ip but i need public ip to run the client from other pc and connect to server for chating. 
    serverSocket.Listen(5) 
    serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing) 
End Sub 

**Client Code** 

Dim clientSocket As Socket 
Dim byteData(1023) As Byte 

Dim PubIP as String = "82.XX.XX.XX" 
Dim LocalIP as String = "192.XX.XX.XX" 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 
    Dim ipAddress As IPAddress = ipAddress.Parse(PubIP) 
    Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, 8080) 
    clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing) 
End Sub 

localip을 사용하여 서버를 실행할 때 제대로 실행되고 내 컴퓨터에서만 클라이언트를 연결할 수 있습니다 (서버가 실행되는 곳). 다른 컴퓨터에서 클라이언트를 실행하려고하면 서버에 연결되지 않습니다.소켓 클라이언트와 서버가 작동하지 않습니다

공개 IP를 할당했지만 프로그램을 실행하면 Visual Studio에서이 오류를 표시합니다. 요청한 주소가 해당 컨텍스트에서 유효하지 않습니다.

그래서 여기 붙어 있습니다. 나는 내 컴퓨터에서 실행하는 경우에만 클라이언트에 연결할 수 있지만 내 친구 PC에서 내 PC에 내 PC에 클라이언트를 연결해야합니다.

다른 대안을 환영합니다.

답변

관련 문제