• Welcome to Valhalla Legends Archive.
 

Sockets

Started by Mangix, September 17, 2005, 07:04 PM

Previous topic - Next topic

Mangix

im having a little trouble with the sockets. im trying to use IPEndPoint but i need some way of resolving an address to an IP. anyone know how to do this?

edit:also how can i use VB's Chr Function in .NET?

MyndFyre

You don't want to use VB's Chr function in .NET, it returns a System.Char type, which is 16 bit.  You need to be more specific with your question for me to tell you what you should use instead.

IPEndPoint's constructor accepts an IPAddress structure.  An array of IPAddress structures can be obtained for a given domain name or dotted-quad address string via Dns.Resolve(string).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

ahhh i see. thanks

also about the Chr function, i need to use it because in a TextBox's KeyPressed Event, the e.KeyChar has to be a Char and i dont know how to return it without Chr. but Chr only seems to work in VB .NET

also 2 more things. my code for making a socket is currecntly System.Net.Sockets.Socket sckBNCS = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPHostEntry hostEntry = System.Net.Dns.Resolve("localhost");
System.Net.IPAddress[] ipAddresses = new hostEntry.AddressList;
System.Net.IPEndPoint ipNum = new System.Net.IPEndPoint(ipAddresses[0], 6112);
the IPAddress[] line errors though. it says "A new Expression Requires () or [] after type" just so you know it isnt in a void/function because if i do use it in a void/function, then it's encapsulated and i cant use it in other void/function which i need to do.

my code for making a Listening client is this System.Net.Sockets.Socket sckServer = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.Sockets.TcpListener sckLst = new System.Net.Sockets.TcpListener(6112);
sckLst.Start();
it doesnt error but i havent tried it out since i cant build the main form(which shows the server form)

MyndFyre

You don't need the new keyword.  Just hostEntry.AddressList; will do.  You're not making a new object.

Uhhh....  You don't need to do anything besides "e.KeyChar".  It returns a char.  You don't assign to it.  For example:

char someCharacterThatWasPressed = e.KeyChar;


Finally, I'd suggest making the listener on a separate thread.  However, note that you don't LISTEN with a Bnet connection unless you're making a server (which is not a client).  You'd want TcpClient.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

#4
nonono. i meant something like this. if (e.KeyChar == Chr(13)) that's why i need to know how.

and also if i remove the new keyword from the hostEntry.AddressList, then it returns 4 errors. with it only 1 which is the one i mentioned.

edit:lol that's a nice way to find it :)

edit2: ok the first Socket which is the connecting one is on frmMain. the second one is on frmServer.

and i know i dont Listen when data comes. that's for servers. also are you saying that im supposed to use TcpClient to connect to myself? if so how?

edit:ok i know why the error occurs but i dont know why. here is my current code(i rewrote it and used the 'using' statement).
public Socket sckBNCS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public IPHostEntry hostEntry = Dns.Resolve("localhost");
public IPAddress[] ipAddresses = hostEntry.AddressList;
public IPEndPoint ipNum = new IPEndPoint(ipAddresses[0], 6112);
line #3 errors. it's expecting a class but instead it's a field. what i cant understand is why a class is expected.

MyndFyre

Take the "public" keywords off of all of those declarations.  Just

Socket sckBNCS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry hostEntry = Dns.Resolve("localhost");
IPAddress[] ipAddresses = hostEntry.AddressList;
IPEndPoint ipNum = new IPEndPoint(ipAddresses[0], 6112);


You only use public/private/protected/internal on class-level members.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

well i fixed that. right now my connection functions/voids are in a class and it doesnt error. and yeah i took em off cause i didnt need em anymore.

right now i need a way to know how to open another form. i tried frmServer.ActiveForm.Show(); and frmServer.ActiveForm.Activate();

none of em work.

Also, im wondering what to put in a Byte Array. if i have a Function that has a byte array, i need to know what to put in it because it considers regular numbers to be integers.

MyndFyre


frmServer fs = new frmServer();
fs.Show();

You need to be more specific about your byte[] question.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

ok let's say i have a sub g like sooooooooo void g(byte[] b)
{
   //some code
}


now my question is...what do i put in the first argument?

MyndFyre

What does the function documentation say to put in it?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

huh? this is just a sample function that i just made up.

MyndFyre

Well then how am I supposed to know what's supposed to be in the byte array?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

ok i'll try to make things clearer then. i dont know what a byte array is. the first time i've heard of it was when i tried using the BitConverter.

ok so i now have 2 questions. what's supposed to be in the byte[] in the first argument of BitConverter.ToUInt32 and what's supposed to be there in the Socket.Connect function.

MyndFyre

Quote from: Mangix on September 18, 2005, 11:41 PM
ok i'll try to make things clearer then. i dont know what a byte array is. the first time i've heard of it was when i tried using the BitConverter.

ok so i now have 2 questions. what's supposed to be in the byte[] in the first argument of BitConverter.ToUInt32 and what's supposed to be there in the Socket.Connect function.

To question 1: a list of at least 4 bytes that you want to convert from bytes into a uint.  To question 2: an IPEndPoint object.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

erm whoops. for question 2 i meant Socket.Send()

my mistake. also for the BitConverter, will the number 4 work?