I make a AxWinSock project, and when I connect with a IP:Port, pop up this error:
HRESULT: 0x800a9c46
Help me please D:
Printable View
I make a AxWinSock project, and when I connect with a IP:Port, pop up this error:
HRESULT: 0x800a9c46
Help me please D:
.NET provides a whole namespace for working with sockets, i.e. System.Net.Sockets. You really should be using that namespace to write network code in .NET. If you have issues with it then you'll get more descriptive exceptions.
I already know it, but i have seen people with it and have the same error.
Help please D:
HRESULT 0x800a9c46 translates into this error message:
"Wrong protocol or connection state for the requested transaction or request"
And how can i solve it? Thanks a lot :D
My code is very basic, i'm starting with WinSock:
Code:Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
AxWinsock1.RemoteHost = TextBox1.Text
AxWinsock1.RemotePort = TextBox2.Text
AxWinsock1.Connect()
End Sub
Private Sub AxWinsock1_ConnectEvent(sender As Object, e As System.EventArgs) Handles AxWinsock1.ConnectEvent
Label1.Text = "CONNECTED"
Label1.ForeColor = Color.Green
AxWinsock1.SendData("Connected!")
Label1.Location = New Point(195, 247)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
AxWinsock1.SendData(TextBox3.Text)
End Sub
Private Sub AxWinsock1_ConnectionRequest(sender As Object, e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles AxWinsock1.ConnectionRequest
AxWinsock1.Close()
AxWinsock1.Accept(e.requestID)
End Sub
Private Sub AxWinsock1_DataArrival(sender As Object, e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles AxWinsock1.DataArrival
Dim data As String = ""
AxWinsock1.GetData(data)
RichTextBox1.Text &= (data & vbNewLine)
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
AxWinsock1.Listen()
End Sub
End Class
As jmcilhinney already suggested. Start by replacing the ActiveX component with the .Net classes.
I didn't find a nice tutorial of it, but I'll search better.
With .Net classes I'll have the same error. I read, searching this error in google, somebody who have it but with .net classes :S.
Thanks again.
I have the code, and now tell me that the host expressly reject the connection.
And, in some IPs that connect, when I send something, tell me that the host force the end of connection.
Code:
Thanks a lot, again.Code:Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class Form1
Dim Client As New TcpClient
Dim Stream As NetworkStream
Dim SendData(1000) As Byte
Dim ReceiveData(1000) As Byte
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Client.Connect(TextBox1.Text, TextBox2.Text)
Stream = Client.GetStream
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
SendData = Encoding.ASCII.GetBytes(TextBox3.Text)
Stream.Write(SendData, 0, SendData.Length)
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If Client.GetStream.DataAvailable Then
Stream.Read(ReceiveData, 0, 1000)
RichTextBox1.Text = "Server: " & Encoding.ASCII.GetString(ReceiveData)
End If
If Client.Connected = True Then
Label1.Text = "CONNECTED"
Label1.ForeColor = Color.LimeGreen
Label1.Location = New Point(195, 247)
End If
End Sub
End Class
PD: I already had Firewall and Antivirus turned off.
So the server is rejecting your connection. So you're either trying to connect to the wrong connection or there's something wrong on the server side. There is no way of telling since all we see is that you're trying to connect to TextBox1.Text and you're relying on an implicit conversion of TextBox2.Text to an integer.
You are telling that the IP or Port are wrong?
And it?: "And, in some IPs that connect, when I send something, tell me that the host force the end of connection."
Sorry, Ii'm noob in this field and I'm spanish. Thanks for your patience :o
No, what I'm telling you is that the exception you say that you get tells you that the server has rejected your connection. I don't know if the IP or Port are wrong since I have no idea what you have in your textboxes.
But ALL IP's that I test tell me it, except the IP's of game servers (Like minecraft, etc..) But when try send something tell me:
"The host force the end of connection."
If you can't tell us what you're trying to do then it's rather difficult to tell you what you're doing wrong. All communication must follow a specific protocol and there is no way of knowing what kind of protocol you're trying to follow. You can't just open up a random port number and then send any kind of information to that port. For example if you try to send something on port 80 there must be a web server on the other end and that is adhering to the HTTP protocol after you've done the initial handshaking you must send text that is understood by that protocol.
I'm trying to connect to a web (IP:Port). I use port 80 and don't tell me that the host expressly reject the connection.
Then, when I connect, what have to send?
Well, it depends on what you want to get back. Here's a quick example that access www.microsoft.com:
However if you want to access web servers you should really look into the WebClient class instead of using TcpClient.Code:Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim client As New TcpClient()
client.Connect("www.microsoft.com", 80)
Dim textToSend As String = "GET / HTTP/1.1" & vbCrLf & _
"Host: microsoft.com" & vbCrLf & _
vbCrLf
Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(textToSend)
Using stream As NetworkStream = client.GetStream()
stream.Write(data, 0, data.Length)
data = New Byte(1024) {}
Dim bytes As Integer = 0
Dim responseText As String = ""
Do
bytes = stream.Read(data, 0, data.Length)
responseText &= System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Loop While stream.DataAvailable
MessageBox.Show(responseText)
End Using
client.Close()
End Sub
I don't understand this: (don't know what is)
The fails is that I don't recive the server answers.Code:"GET / HTTP/1.1" & vbCrLf & _
"Host: microsoft.com" & vbCrLf & _
vbCrLf
That is a barebone HTTP GET request. Basically that is what your web browser would send to the web server if you navigate to http://www.microsoft.com (in reality the web browser would send more information than the above, but this is the minimum you need to send). The web server will then reply by sending you the page you have requested.
When you say that you don't receive the server answer I'll bet that you actually didn't try my example. Start a new project, put a button on the form and paste in my code and run it. You'll get a message box showing the answer from the server (if you're connected to the Internet at the time that is).
You still haven't told us what you really want to do and what you've tried. Your explanation this far has been similar to this:
I call someone on the phone.
I then say a few random words in Swahili.
They just hang up.
Why do they do that?
I encourage you to read up on the HTTP protocol, if that is indeed what you want to learn. Just Google it and you get a bunch of results.
I test it, when MsgBox appear means that it has connected with web, yes?
I want that connect with google.com, for example. How can I make?
I don't know because you still haven't told us what you want to do. You can either use my example and read up a bit more yourself or explain what you want to do. We need to know what you want to send to the server and what kind of result you expect to get back. I can't read your mind. Tell us how your program is supposed to work.
As I've already mentioned; if you want to communicate via HTTP then using the WebClient is a better choice than to use the TcpClient.