Author | Message | Time |
---|---|---|
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? | September 18, 2005, 12:04 AM |
Myndfyr | 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). | September 18, 2005, 9:03 AM |
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 [code]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);[/code] 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 [code]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();[/code] it doesnt error but i havent tried it out since i cant build the main form(which shows the server form) | September 18, 2005, 3:05 PM |
Myndfyr | 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: [code] char someCharacterThatWasPressed = e.KeyChar; [/code] 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. | September 18, 2005, 7:45 PM |
Mangix | nonono. i meant something like this. [code]if (e.KeyChar == Chr(13))[/code] 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). [code] 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);[/code] line #3 errors. it's expecting a class but instead it's a field. what i cant understand is why a class is expected. | September 18, 2005, 8:18 PM |
Myndfyr | Take the "public" keywords off of all of those declarations. Just [code] 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); [/code] You only use public/private/protected/internal on class-level members. | September 18, 2005, 11:59 PM |
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. | September 19, 2005, 12:11 AM |
Myndfyr | [code] frmServer fs = new frmServer(); fs.Show(); [/code] You need to be more specific about your byte[] question. | September 19, 2005, 12:49 AM |
Mangix | ok let's say i have a sub g like sooooooooo [code]void g(byte[] b) { //some code }[/code] now my question is...what do i put in the first argument? | September 19, 2005, 12:54 AM |
Myndfyr | What does the function documentation say to put in it? | September 19, 2005, 1:04 AM |
Mangix | huh? this is just a sample function that i just made up. | September 19, 2005, 1:05 AM |
Myndfyr | Well then how am I supposed to know what's supposed to be in the byte array? | September 19, 2005, 3:43 AM |
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. | September 19, 2005, 4:41 AM |
Myndfyr | [quote author=Mangix link=topic=12841.msg128663#msg128663 date=1127104862] 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. [/quote] 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. | September 19, 2005, 6:22 AM |
Mangix | erm whoops. for question 2 i meant Socket.Send() my mistake. also for the BitConverter, will the number 4 work? | September 19, 2005, 6:42 AM |
Myndfyr | Mangix, you have to ask yourself, why are you trying to change a 4 into a 4? Pretty much, the BitConverter class exists to provide a type- and memory-safe way of doing this: [code] uint val; byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // this is BitConverter's magic: fixed (byte *pBuf = &buffer) { pBuf += 2; // move the pointer to the third element in the array uint* pBufferAsUint = (uint*)pBuf; val = *pBufferAsUint; } [/code] This is the same as doing: [code] uint val; byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; val = BitConverter.ToUInt32(buffer, 2); [/code] In either scenario, the variable val holds the value 0x06050403. But, by using the second type of code, you increase code safety, prevent memory leaks, and increase code verifiablity (this is what .NET was designed for). Best of all, you can avoid pointer use. For question 2.... you send the data you want to send to the other end of the connection? I really don't know what you want me to say besides the obvious. I've cited this buffer on numerous occasions -- you have to inherit from it, but if you made your own, this is a good model. Example: [code] /// <summary> /// The collection of bytes to send. /// </summary> private MemoryStream m_data; [/code] The MemoryStream class is somewhat like a pointer reader/writer when combined with the BinaryReader and BinaryWriter classes (all from the System.IO namespace). In fact, the buffer I provided doesn't even use BinaryReader/BinaryWriter -- it uses BitConverter to transform data into byte arrays (I think that could be improved upon). But pretty much, it works like this: 1.) Create a MemoryStream 2.) Write to the MemoryStream. The current position in the stream is advanced by as many bytes as were written. 3.) Repeat step 2 until all data is written. 4.) Obtain the data in the MemoryStream in a byte[]. 5.) Send the data via the Send() method. | September 19, 2005, 3:45 PM |
Mangix | in C#, every time i enter a number, it considers it to be of type int even though i set it to be something else. dunno why -_-. also thank's for the byte[] thing. as for the socket.Send, nice explination but i still dont get something. the BitConverter class cannot make a byte array out of a string(obviously) so is there any way to do that? | September 19, 2005, 10:44 PM |
Myndfyr | No. Treat numbers as numbers, not as strings like you do in VB. Treating them as strings is the only way to make a somewhat-efficient buffer. If you need to, I know the C# compiler has suffixes for every primitive type (for example, to make a uint, use [code] uint someNum = 0xf8329fcdu; [/code] -- note the "u" suffix at the end of the number). For bytes, I don't know what the suffix is, but you can just cast: [code] byte someNum = (byte)0xcd; [/code] Note that, using hex notation, you can't have a value greater than 0xff, or if using decimal, more than 255. | September 19, 2005, 10:57 PM |
Mangix | isnt it 256? and i dont make em as strings. i do something like this [code]byte g = 4;[/code] and it immediatly considers it to be an integer. also does (byte) even work? i've tried doing the same method of converting types to another type but it never works. | September 20, 2005, 12:52 AM |
Myndfyr | [quote author=Mangix link=topic=12841.msg128733#msg128733 date=1127177528] isnt it 256? [/quote] No. A byte can store 256 unique values from 0 to 0xff or 255. [quote author=Mangix link=topic=12841.msg128733#msg128733 date=1127177528] and i dont make em as strings. i do something like this [code]byte g = 4;[/code] and it immediatly considers it to be an integer. also does (byte) even work? i've tried doing the same method of converting types to another type but it never works. [/quote] So try [code]byte g = (byte)4;[/code]. Sometimes casting doesn't work, but that's if you're using nonprimitive or uncastable types. For example: [code] uint f = 0x400583; byte b = (byte)f; [/code] That will raise a compiler error because you can lose information in the conversion. You could: [code] uint f = 0x400583; byte b = (byte)(f & 0xff); [/code] which would yield the result you're looking for. Also, conversion between signed/unsigned is often erroneous. For example: [code] int f = -1; // -1 is 0xffffffff uint g = (uint)f; // compiler exception [/code] Proper way to do this is via unchecked in one of the following ways: [code] int f = -1; uint g; g = unchecked((uint)f); // or unchecked { g = (uint)f; } [/code] | September 20, 2005, 1:52 AM |