<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>CodeGuru Forums - Network Programming</title>
		<link>http://forums.codeguru.com/</link>
		<description>Chat about all aspects of network programming, including raw sockets, Winsock API, BSD sockets, MFC-based CSocket,  CAsyncSocket, and other network-related topics.</description>
		<language>en</language>
		<lastBuildDate>Tue, 21 May 2013 22:08:52 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://forums.codeguru.com/images/misc/rss.png</url>
			<title>CodeGuru Forums - Network Programming</title>
			<link>http://forums.codeguru.com/</link>
		</image>
		<item>
			<title>winsock... binding to WRONG IP byte order WORKS!</title>
			<link>http://forums.codeguru.com/showthread.php?537043-winsock...-binding-to-WRONG-IP-byte-order-WORKS!&amp;goto=newpost</link>
			<pubDate>Thu, 16 May 2013 16:58:55 GMT</pubDate>
			<description><![CDATA[I was updating a wrapper 32 bit windows DLL on an old winsock utilities library, to allow for the case where I need to specifically bind() my sockets to one or another IP addresses, on a multi homed system. I'm working with Visual studio 2008, on XP pro if it matters. 

Anyway, previously I just would not bother doing a bind, just allowing winsock to choose an appropriate IP. But now I was doing something like the below. (Assume dwMyIP is a ULONG containing a known good IP, in host order.)



Code:
---------
struct sockaddr_in sock_bind_addr;
 
sock_bind_addr.sin_family = AF_INET;  
sock_bind_addr.sin_addr.S_un.S_addr = htonl(dwMyIp); 
sock_bind_addr.sin_port = 0;   // 0 allows winsock to pick a port.
   
if (bind(*pSockToStartWith, (struct sockaddr *)(&sock_bind_addr), sizeof(struct sockaddr_in)) == SOCKET_ERROR) {

... (Houston, we have a problem)'
 return -1;
}
---------
So anyway, the bind would fail, and on a whim I decided to 
remove the htonl() call, and store the 32 bit address in host order in the sockaddr_in  structure. It WORKED! 

Now I know everyone will jump to tell me that obviously I was missing something, and that my address was already in network order. Well consider this... the network IP in question was "192.168.0.190" Single step debugging though the code reveals that at this point in the program, dwMyIp = 0xBE00A8C0, which as an IP address is nothing like mine (makes sense... since I'm on an intel/windows machine). If, however I run my dwMyIP through htonl(), I get 0xC0A800BE, which indeed equates to 192.168.0.190. But that won't work, and I get an error (error 10049). :mad:

Further, after completing the bind the "wrong" way (with my host order IP), I can connect to a test application on another machine, which does indeed report the incoming connection from 192.168.0.190. Now I repeated this test with the ULONG equivalent of every available IP IP I'm set up on my system and the result is the same. I can bind to any valid address as long as I store it in host order, not network order. 

OK...getting something to work the WRONG way and ignoring the problem is a prescription for a disaster sooner or later. So I'm about to make a hidden configuration variable for this to take care of both cases. But can anyone tell me why this is happening? Is there a known problem with the winsock (winsock 2 if it matters) bind() function? I can tell you from using the TCP/IP server variation, where I DO have to specify a port to connect to, that the expected behavior there is correct (meaning, if I want a client to connect to me at port 1234, I *DO* have to bind my listening socket to htons(1234). 

As far as my server goes, its currently bound to IP_ANY, which in windows equates to 0. I have not yet tested to to see how it responds to a host format (little endian) IP, and I'll do that next. But I thought it was a good stopping point and a good time to ask.

Thanks for any help!]]></description>
			<content:encoded><![CDATA[<div>I was updating a wrapper 32 bit windows DLL on an old winsock utilities library, to allow for the case where I need to specifically bind() my sockets to one or another IP addresses, on a multi homed system. I'm working with Visual studio 2008, on XP pro if it matters. <br />
<br />
Anyway, previously I just would not bother doing a bind, just allowing winsock to choose an appropriate IP. But now I was doing something like the below. (Assume dwMyIP is a ULONG containing a known good IP, in host order.)<br />
<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">struct sockaddr_in sock_bind_addr;<br />
&nbsp;<br />
sock_bind_addr.sin_family = AF_INET;&nbsp; <br />
sock_bind_addr.sin_addr.S_un.S_addr = htonl(dwMyIp); <br />
sock_bind_addr.sin_port = 0;&nbsp;  // 0 allows winsock to pick a port.<br />
&nbsp;  <br />
if (bind(*pSockToStartWith, (struct sockaddr *)(&amp;sock_bind_addr), sizeof(struct sockaddr_in)) == SOCKET_ERROR) {<br />
<br />
... (Houston, we have a problem)'<br />
&nbsp;return -1;<br />
}</code><hr />
</div> So anyway, the bind would fail, and on a whim I decided to <br />
remove the htonl() call, and store the 32 bit address in host order in the sockaddr_in  structure. It WORKED! <br />
<br />
Now I know everyone will jump to tell me that obviously I was missing something, and that my address was already in network order. Well consider this... the network IP in question was &quot;192.168.0.190&quot; Single step debugging though the code reveals that at this point in the program, dwMyIp = 0xBE00A8C0, which as an IP address is nothing like mine (makes sense... since I'm on an intel/windows machine). If, however I run my dwMyIP through htonl(), I get 0xC0A800BE, which indeed equates to 192.168.0.190. But that won't work, and I get an error (error 10049). :mad:<br />
<br />
Further, after completing the bind the &quot;wrong&quot; way (with my host order IP), I can connect to a test application on another machine, which does indeed report the incoming connection from 192.168.0.190. Now I repeated this test with the ULONG equivalent of every available IP IP I'm set up on my system and the result is the same. I can bind to any valid address as long as I store it in host order, not network order. <br />
<br />
OK...getting something to work the WRONG way and ignoring the problem is a prescription for a disaster sooner or later. So I'm about to make a hidden configuration variable for this to take care of both cases. But can anyone tell me why this is happening? Is there a known problem with the winsock (winsock 2 if it matters) bind() function? I can tell you from using the TCP/IP server variation, where I DO have to specify a port to connect to, that the expected behavior there is correct (meaning, if I want a client to connect to me at port 1234, I *DO* have to bind my listening socket to htons(1234). <br />
<br />
As far as my server goes, its currently bound to IP_ANY, which in windows equates to 0. I have not yet tested to to see how it responds to a host format (little endian) IP, and I'll do that next. But I thought it was a good stopping point and a good time to ask.<br />
<br />
Thanks for any help!</div>

 ]]></content:encoded>
			<category domain="http://forums.codeguru.com/forumdisplay.php?62-Network-Programming">Network Programming</category>
			<dc:creator>Randy C</dc:creator>
			<guid isPermaLink="true">http://forums.codeguru.com/showthread.php?537043-winsock...-binding-to-WRONG-IP-byte-order-WORKS!</guid>
		</item>
		<item>
			<title>Need help on Rc2</title>
			<link>http://forums.codeguru.com/showthread.php?536997-Need-help-on-Rc2&amp;goto=newpost</link>
			<pubDate>Wed, 15 May 2013 01:59:10 GMT</pubDate>
			<description><![CDATA[I have to make the encrypted comunication between server and client ,where to encrypt the message we use RC2 and the secret key is protected by RSA.
 
My code for the client is :

Code:
---------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.IO;
namespace PjesaKLIENTI_TCP
{
public class Klienti
{
scopes(fusheveprimit)te Main-it
static Socket S_Klienti;
static byte[] bufferi = new byte[4 * 1024];
static int prn;
 
static void Main(string[] args)
{
 


 
S_Klienti = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint Ip_Klienti = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4000);
 

try
{
S_Klienti.Connect(Ip_Klienti);
}
catch (SocketException SE)
{

Console.Write("\nE pamundur per t'u lidhur me server. " + SE);
return;
}
string KPbS;
prn = S_Klienti.Receive(bufferi);
KPbS = Encoding.UTF8.GetString(bufferi, 0, prn);
Console.WriteLine("U pranua celesi publik i serverit");
 
RSACryptoServiceProvider rsaK = new RSACryptoServiceProvider();
rsaK.FromXmlString(KPbS);
Console.WriteLine("U importua celesi pubik i serverit");

}
public static void DERGO_HYRJEN_NGA_CONSOLA(string Hyrja_Tastieres)
{
try
{
S_Klienti.Send(Encoding.ASCII.GetBytes(Hyrja_Tastieres));
}
catch
{
Console.WriteLine("Nuk mund te dergohet hyrja ,Ju lutemi kontrolloni edhe nje here konektimin\n\n");
 
}
 
}
 

public static string MERRE_PERGJIGJEN_NGASERVERI()
{
byte[] E_Dhena_Nga_Serveri = new byte[1024];
int Gjatesia_Dhenes = S_Klienti.Receive(E_Dhena_Nga_Serveri);
string E_Dhena_Perfundimtare = Encoding.ASCII.GetString(E_Dhena_Nga_Serveri, 0, Gjatesia_Dhenes);
 
return E_Dhena_Perfundimtare;
}
public static string DecryptTextFromFile(string mesazhi)
{
return "";
}

}
}
==========================================================================================
And the code for the server is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.IO;
 
namespace PjesaSERVERI_TCP
{
public class Serveri
{
 
public static IPEndPoint ipKlienti;
public static TcpListener TCP_Listener;
private int port = 9999;
static Socket KLIENTI;

 
static void Main(string[] args)
{
 

Serveri s = new Serveri();
 

Socket S_FIllimiLidhjes = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
IPEndPoint IP = new IPEndPoint(IPAddress.Any, 4000);//Lidhet me cilindo klient qe shtron kerkese
 

S_FIllimiLidhjes.Bind(IP);
S_FIllimiLidhjes.Listen(20);
 
Console.WriteLine("\n\nServeri eshte duke pritur per ndonje klient!\n");

KLIENTI = S_FIllimiLidhjes.Accept();
 
IPEndPoint ipKLienti = (IPEndPoint)KLIENTI.RemoteEndPoint;
Console.WriteLine("\nServeri u lidhe me klientin{0} ne portin {1}", ipKLienti.Address, ipKLienti.Port);
 

 
RSACryptoServiceProvider rsaS = new RSACryptoServiceProvider();
string KpbS = rsaS.ToXmlString(false);
KLIENTI.Send(Encoding.UTF8.GetBytes(KpbS));
Console.WriteLine("U dergua celesi publik tek:{0}", ipKlienti.Address);
string mesahi = "jepni nje tekst";
KTHEJE_PERGJIGJEN_TEK_KLIENTI(mesahi);
string teksti = MERRE_TE_DHENEN_E_PROCESUAR();
string ekriptuar=Enkripto(teksti,"17");
 

 
}
 
public static void KTHEJE_PERGJIGJEN_TEK_KLIENTI(string Pergjigja_kthyer)
{
 
byte[] B_Finale = Encoding.ASCII.GetBytes(Pergjigja_kthyer);
//kthejme pergjgjen
KLIENTI.Send(B_Finale, B_Finale.Length, SocketFlags.None);
 
}
 
public static string MERRE_TE_DHENEN_E_PROCESUAR()
{
byte[] B_Dhena = new byte[2000];
int Gjatesia_Te_Dhenes = KLIENTI.Receive(B_Dhena);
string Server_Dhena = Encoding.ASCII.GetString(B_Dhena, 0, Gjatesia_Te_Dhenes);
return Server_Dhena;
 
}

public static string Enkripto(string message, string inkey)
{
bool file = false;
byte[] b = null;
 
//if (message == null) return;
//if (inkey == null) return;
try
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
 
RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider();
 

if (inkey == "") { inkey = "test123"; }
 
rc2.Key = StringToByte(inkey, 12); // convert to 12 characters
 
rc2.IV = StringToByte("", 8);
byte[] key = rc2.Key;
byte[] IV = rc2.IV;
 
ICryptoTransform encryptor = rc2.CreateEncryptor(key, IV);
 
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
 
// Write all data to the crypto stream and flush it.
 
if (file == false)
{
csEncrypt.Write(StringToByte(message), 0, StringToByte(message).Length);
csEncrypt.FlushFinalBlock();
}
else
{
 
csEncrypt.Write(b, 0, b.Length);
csEncrypt.FlushFinalBlock();
}
 
// Get the encrypted array of bytes.
byte[] encrypted1 = msEncrypt.ToArray();
 
string encrypted = ByteToString(encrypted1);
 
ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV);
 
// Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted1);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
 
string decrypted = ByteToString(csDecrypt);
return decrypted;
}
catch (Exception ex)
{
string encrypted = ex.Message.ToString();
return encrypted;

} 
}
public static byte[] StringToByte(string StringToConvert, int length)
{
 
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static byte[] StringToByte(string StringToConvert)
{
 
char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[CharArray.Length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static string ByteToString(CryptoStream buff)
{
string sbinary = "";
int b = 0;
do
{
b = buff.ReadByte();
if (b != -1) sbinary += ((char)b);
 
} while (b != -1);
return (sbinary);
}
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); // hex format
}
return (sbinary);
}

 

 

}
 
}
---------
 
===
That was my code so far..I just can't make it work.]]></description>
			<content:encoded><![CDATA[<div>I have to make the encrypted comunication between server and client ,where to encrypt the message we use RC2 and the secret key is protected by RSA.<br />
 <br />
My code for the client is :<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Security.Cryptography;<br />
using System.IO;<br />
namespace PjesaKLIENTI_TCP<br />
{<br />
public class Klienti<br />
{<br />
scopes(fusheveprimit)te Main-it<br />
static Socket S_Klienti;<br />
static byte[] bufferi = new byte[4 * 1024];<br />
static int prn;<br />
&nbsp;<br />
static void Main(string[] args)<br />
{<br />
&nbsp;<br />
<br />
<br />
&nbsp;<br />
S_Klienti = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br />
IPEndPoint Ip_Klienti = new IPEndPoint(IPAddress.Parse(&quot;127.0.0.1&quot;), 4000);<br />
&nbsp;<br />
<br />
try<br />
{<br />
S_Klienti.Connect(Ip_Klienti);<br />
}<br />
catch (SocketException SE)<br />
{<br />
<br />
Console.Write(&quot;\nE pamundur per t'u lidhur me server. &quot; + SE);<br />
return;<br />
}<br />
string KPbS;<br />
prn = S_Klienti.Receive(bufferi);<br />
KPbS = Encoding.UTF8.GetString(bufferi, 0, prn);<br />
Console.WriteLine(&quot;U pranua celesi publik i serverit&quot;);<br />
&nbsp;<br />
RSACryptoServiceProvider rsaK = new RSACryptoServiceProvider();<br />
rsaK.FromXmlString(KPbS);<br />
Console.WriteLine(&quot;U importua celesi pubik i serverit&quot;);<br />
<br />
}<br />
public static void DERGO_HYRJEN_NGA_CONSOLA(string Hyrja_Tastieres)<br />
{<br />
try<br />
{<br />
S_Klienti.Send(Encoding.ASCII.GetBytes(Hyrja_Tastieres));<br />
}<br />
catch<br />
{<br />
Console.WriteLine(&quot;Nuk mund te dergohet hyrja ,Ju lutemi kontrolloni edhe nje here konektimin\n\n&quot;);<br />
&nbsp;<br />
}<br />
&nbsp;<br />
}<br />
&nbsp;<br />
<br />
public static string MERRE_PERGJIGJEN_NGASERVERI()<br />
{<br />
byte[] E_Dhena_Nga_Serveri = new byte[1024];<br />
int Gjatesia_Dhenes = S_Klienti.Receive(E_Dhena_Nga_Serveri);<br />
string E_Dhena_Perfundimtare = Encoding.ASCII.GetString(E_Dhena_Nga_Serveri, 0, Gjatesia_Dhenes);<br />
&nbsp;<br />
return E_Dhena_Perfundimtare;<br />
}<br />
public static string DecryptTextFromFile(string mesazhi)<br />
{<br />
return &quot;&quot;;<br />
}<br />
<br />
}<br />
}<br />
==========================================================================================<br />
And the code for the server is :<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Threading;<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.Security.Cryptography;<br />
using System.IO;<br />
&nbsp;<br />
namespace PjesaSERVERI_TCP<br />
{<br />
public class Serveri<br />
{<br />
&nbsp;<br />
public static IPEndPoint ipKlienti;<br />
public static TcpListener TCP_Listener;<br />
private int port = 9999;<br />
static Socket KLIENTI;<br />
<br />
&nbsp;<br />
static void Main(string[] args)<br />
{<br />
&nbsp;<br />
<br />
Serveri s = new Serveri();<br />
&nbsp;<br />
<br />
Socket S_FIllimiLidhjes = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br />
&nbsp;<br />
IPEndPoint IP = new IPEndPoint(IPAddress.Any, 4000);//Lidhet me cilindo klient qe shtron kerkese<br />
&nbsp;<br />
<br />
S_FIllimiLidhjes.Bind(IP);<br />
S_FIllimiLidhjes.Listen(20);<br />
&nbsp;<br />
Console.WriteLine(&quot;\n\nServeri eshte duke pritur per ndonje klient!\n&quot;);<br />
<br />
KLIENTI = S_FIllimiLidhjes.Accept();<br />
&nbsp;<br />
IPEndPoint ipKLienti = (IPEndPoint)KLIENTI.RemoteEndPoint;<br />
Console.WriteLine(&quot;\nServeri u lidhe me klientin{0} ne portin {1}&quot;, ipKLienti.Address, ipKLienti.Port);<br />
&nbsp;<br />
<br />
&nbsp;<br />
RSACryptoServiceProvider rsaS = new RSACryptoServiceProvider();<br />
string KpbS = rsaS.ToXmlString(false);<br />
KLIENTI.Send(Encoding.UTF8.GetBytes(KpbS));<br />
Console.WriteLine(&quot;U dergua celesi publik tek:{0}&quot;, ipKlienti.Address);<br />
string mesahi = &quot;jepni nje tekst&quot;;<br />
KTHEJE_PERGJIGJEN_TEK_KLIENTI(mesahi);<br />
string teksti = MERRE_TE_DHENEN_E_PROCESUAR();<br />
string ekriptuar=Enkripto(teksti,&quot;17&quot;);<br />
&nbsp;<br />
<br />
&nbsp;<br />
}<br />
&nbsp;<br />
public static void KTHEJE_PERGJIGJEN_TEK_KLIENTI(string Pergjigja_kthyer)<br />
{<br />
&nbsp;<br />
byte[] B_Finale = Encoding.ASCII.GetBytes(Pergjigja_kthyer);<br />
//kthejme pergjgjen<br />
KLIENTI.Send(B_Finale, B_Finale.Length, SocketFlags.None);<br />
&nbsp;<br />
}<br />
&nbsp;<br />
public static string MERRE_TE_DHENEN_E_PROCESUAR()<br />
{<br />
byte[] B_Dhena = new byte[2000];<br />
int Gjatesia_Te_Dhenes = KLIENTI.Receive(B_Dhena);<br />
string Server_Dhena = Encoding.ASCII.GetString(B_Dhena, 0, Gjatesia_Te_Dhenes);<br />
return Server_Dhena;<br />
&nbsp;<br />
}<br />
<br />
public static string Enkripto(string message, string inkey)<br />
{<br />
bool file = false;<br />
byte[] b = null;<br />
&nbsp;<br />
//if (message == null) return;<br />
//if (inkey == null) return;<br />
try<br />
{<br />
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();<br />
&nbsp;<br />
RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider();<br />
&nbsp;<br />
<br />
if (inkey == &quot;&quot;) { inkey = &quot;test123&quot;; }<br />
&nbsp;<br />
rc2.Key = StringToByte(inkey, 12); // convert to 12 characters<br />
&nbsp;<br />
rc2.IV = StringToByte(&quot;&quot;, 8);<br />
byte[] key = rc2.Key;<br />
byte[] IV = rc2.IV;<br />
&nbsp;<br />
ICryptoTransform encryptor = rc2.CreateEncryptor(key, IV);<br />
&nbsp;<br />
MemoryStream msEncrypt = new MemoryStream();<br />
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);<br />
&nbsp;<br />
// Write all data to the crypto stream and flush it.<br />
&nbsp;<br />
if (file == false)<br />
{<br />
csEncrypt.Write(StringToByte(message), 0, StringToByte(message).Length);<br />
csEncrypt.FlushFinalBlock();<br />
}<br />
else<br />
{<br />
&nbsp;<br />
csEncrypt.Write(b, 0, b.Length);<br />
csEncrypt.FlushFinalBlock();<br />
}<br />
&nbsp;<br />
// Get the encrypted array of bytes.<br />
byte[] encrypted1 = msEncrypt.ToArray();<br />
&nbsp;<br />
string encrypted = ByteToString(encrypted1);<br />
&nbsp;<br />
ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV);<br />
&nbsp;<br />
// Now decrypt the previously encrypted message using the decryptor<br />
MemoryStream msDecrypt = new MemoryStream(encrypted1);<br />
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);<br />
&nbsp;<br />
string decrypted = ByteToString(csDecrypt);<br />
return decrypted;<br />
}<br />
catch (Exception ex)<br />
{<br />
string encrypted = ex.Message.ToString();<br />
return encrypted;<br />
<br />
} <br />
}<br />
public static byte[] StringToByte(string StringToConvert, int length)<br />
{<br />
&nbsp;<br />
char[] CharArray = StringToConvert.ToCharArray();<br />
byte[] ByteArray = new byte[length];<br />
for (int i = 0; i &lt; CharArray.Length; i++)<br />
{<br />
ByteArray[i] = Convert.ToByte(CharArray[i]);<br />
}<br />
return ByteArray;<br />
}<br />
public static byte[] StringToByte(string StringToConvert)<br />
{<br />
&nbsp;<br />
char[] CharArray = StringToConvert.ToCharArray();<br />
byte[] ByteArray = new byte[CharArray.Length];<br />
for (int i = 0; i &lt; CharArray.Length; i++)<br />
{<br />
ByteArray[i] = Convert.ToByte(CharArray[i]);<br />
}<br />
return ByteArray;<br />
}<br />
public static string ByteToString(CryptoStream buff)<br />
{<br />
string sbinary = &quot;&quot;;<br />
int b = 0;<br />
do<br />
{<br />
b = buff.ReadByte();<br />
if (b != -1) sbinary += ((char)b);<br />
&nbsp;<br />
} while (b != -1);<br />
return (sbinary);<br />
}<br />
public static string ByteToString(byte[] buff)<br />
{<br />
string sbinary = &quot;&quot;;<br />
for (int i = 0; i &lt; buff.Length; i++)<br />
{<br />
sbinary += buff[i].ToString(&quot;X2&quot;); // hex format<br />
}<br />
return (sbinary);<br />
}<br />
<br />
&nbsp;<br />
<br />
&nbsp;<br />
<br />
}<br />
&nbsp;<br />
}</code><hr />
</div> ===<br />
That was my code so far..I just can't make it work.</div>

 ]]></content:encoded>
			<category domain="http://forums.codeguru.com/forumdisplay.php?62-Network-Programming">Network Programming</category>
			<dc:creator>adrian1</dc:creator>
			<guid isPermaLink="true">http://forums.codeguru.com/showthread.php?536997-Need-help-on-Rc2</guid>
		</item>
		<item>
			<title><![CDATA[Winsock2 & pthreads: Accepting Multiple Clients?]]></title>
			<link>http://forums.codeguru.com/showthread.php?536807-Winsock2-amp-pthreads-Accepting-Multiple-Clients&amp;goto=newpost</link>
			<pubDate>Tue, 07 May 2013 02:12:28 GMT</pubDate>
			<description><![CDATA[Hey! So I'm still having troubles connecting multiple clients to my server.
 
To break down what I have going:
 I have a server that is supposed to accept multiple clients.. 
Currently, It works with one person.
 While the server is running, One person is allowed to play the game (While connected to the server). If the server is down, the player cannot play (This shows that the server and client responde and work).
 
However, While the server is running and the client joins, the first player is allowed to play. Everyone else's window goes black and says "Not Responding" (Like any other game in which isn't working).
 While these players are in "Not Responding", It still says the client has connected onto the server (But they're not able to play?).
 
My client code is working perfectly, But i'm having troubles accepting multiple clients onto my server. 

Anyone who is willing to help is welcome! Just know that i'm not an expert (Or close) with pthreads or winsock2. Please help fix up my current server code??:
 


Code:
---------
#include <iostream>
#include <winsock2.h>
#include <vector>
#include <process.h>
#include "Included/pthread.h"
//Some things i'm using or will be using in the future

#define MAX_THREADS 5
//Maximum allowed clients

bool gamerunning = true;
//Tells that the app (Server) is running while oppened
bool srvr_connect = false;
//Is the server connected to the internet? (changes later)
int rc;
//Helps create the thread (I think?)

void *ClientThread(void *threadid) {
     std::cout << "Trying to connect players:\n     Thread: ClientThread\n";
     //Checks to see if the client has connected (works)
}
              

int main() {
    //Below is the server info (ends at '//end')
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);
    
    WSAStartup(Version, &wsa);
    
    SOCKET Listen;
    SOCKET Connect;
    
    Listen = socket(AF_INET, SOCK_STREAM, 0);
    Connect = socket(AF_INET, SOCK_STREAM, 0);
    
    SOCKADDR_IN Server;
    
    Server.sin_addr.s_addr = inet_addr("My IP is here");
    //'My IP is here' is not actually the quote. My actual IP is there
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);
    
    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
    
    listen(Listen, 4);
    
    int size = sizeof(Server);
    //End (Ended the; '//Below is the server info')
    
    std::cout << "Your server has been started!\nConnecting...\n";

    //The following are parts of a connecting test (Just to be sure!)
    char* hey = "620";
    char player_y_cr[101];
    int player_y;
    
    char test[102];
    int test_frame = 0;
    char *test2;
    
    pthread_t threads[MAX_THREADS];
    int i = 0;
    //i is used for determining how many players have connected.
    
    while (gamerunning) {
          Connect = accept(Listen, (SOCKADDR*)&Server, &size);
          //Accept the client
          if (i >= 0 && i < MAX_THREADS) { //Tell that the client cannot connect passed the set amount
               rc = pthread_create(&threads[i], NULL, ClientThread, (void *)i);
               std::cout << "Threads: " << i << "\n";
               i +=1;
          }
          if (Connect != NULL) { //If the connection was successful
               std::cout << "Connection Accepted\n";
               srvr_connect = true;
          }
          if (srvr_connect == true) { //If the server is connected to the internet
            if (test_frame == 0) { //Do this only once (Since the '0' changes later)
               std::cout << "Connection Sent!\nConnection Has Been Breached!\nPlayers Are Now Able to Join Your Server!\n";
               send(Connect, hey, sizeof(hey), 0);
               std::cout << "\n\nSent Data: " << hey << " (This is strictly for binding connections)\n";
               test_frame +=1; //Stops the loop of this (as said above)
            }
            recv(Connect, test, sizeof(test), 0);
            std::cout << test << "\n";
          }
    }
    std::cin.get();
    return 0;
}
---------

I had set notes by things to tell what they do! 
Hopefully I can get some more info on how to do this.
 
Maybe fix up my code? xP
 Thanks, Guys!
 I will try my best on understanding most of your replies on this.
 
-LunarB 

P.S: Feel free to give me constructive critisism.
I'm not entirely sure what's wrong with my code, 
As i'm no expert (Or close) with networking multiple clients.]]></description>
			<content:encoded><![CDATA[<div>Hey! So I'm still having troubles connecting multiple clients to my server.<br />
 <br />
To break down what I have going:<br />
 I have a server that is supposed to accept multiple clients.. <br />
Currently, It works with one person.<br />
 While the server is running, One person is allowed to play the game (While connected to the server). If the server is down, the player cannot play (This shows that the server and client responde and work).<br />
 <br />
However, While the server is running and the client joins, the first player is allowed to play. Everyone else's window goes black and says &quot;Not Responding&quot; (Like any other game in which isn't working).<br />
 While these players are in &quot;Not Responding&quot;, It still says the client has connected onto the server (But they're not able to play?).<br />
 <br />
My client code is working perfectly, But i'm having troubles accepting multiple clients onto my server. <br />
<br />
Anyone who is willing to help is welcome! Just know that i'm not an expert (Or close) with pthreads or winsock2. Please help fix up my current server code??:<br />
 <br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<hr /><code class="bbcode_code">#include &lt;iostream&gt;<br />
#include &lt;winsock2.h&gt;<br />
#include &lt;vector&gt;<br />
#include &lt;process.h&gt;<br />
#include &quot;Included/pthread.h&quot;<br />
//Some things i'm using or will be using in the future<br />
<br />
#define MAX_THREADS 5<br />
//Maximum allowed clients<br />
<br />
bool gamerunning = true;<br />
//Tells that the app (Server) is running while oppened<br />
bool srvr_connect = false;<br />
//Is the server connected to the internet? (changes later)<br />
int rc;<br />
//Helps create the thread (I think?)<br />
<br />
void *ClientThread(void *threadid) {<br />
&nbsp; &nbsp;  std::cout &lt;&lt; &quot;Trying to connect players:\n&nbsp; &nbsp;  Thread: ClientThread\n&quot;;<br />
&nbsp; &nbsp;  //Checks to see if the client has connected (works)<br />
}<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
int main() {<br />
&nbsp; &nbsp; //Below is the server info (ends at '//end')<br />
&nbsp; &nbsp; WSAData wsa;<br />
&nbsp; &nbsp; WORD Version = MAKEWORD(2, 1);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; WSAStartup(Version, &amp;wsa);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; SOCKET Listen;<br />
&nbsp; &nbsp; SOCKET Connect;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Listen = socket(AF_INET, SOCK_STREAM, 0);<br />
&nbsp; &nbsp; Connect = socket(AF_INET, SOCK_STREAM, 0);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; SOCKADDR_IN Server;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Server.sin_addr.s_addr = inet_addr(&quot;My IP is here&quot;);<br />
&nbsp; &nbsp; //'My IP is here' is not actually the quote. My actual IP is there<br />
&nbsp; &nbsp; Server.sin_family = AF_INET;<br />
&nbsp; &nbsp; Server.sin_port = htons(100);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; bind(Listen, (SOCKADDR*)&amp;Server, sizeof(Server));<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; listen(Listen, 4);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; int size = sizeof(Server);<br />
&nbsp; &nbsp; //End (Ended the; '//Below is the server info')<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; std::cout &lt;&lt; &quot;Your server has been started!\nConnecting...\n&quot;;<br />
<br />
&nbsp; &nbsp; //The following are parts of a connecting test (Just to be sure!)<br />
&nbsp; &nbsp; char* hey = &quot;620&quot;;<br />
&nbsp; &nbsp; char player_y_cr[101];<br />
&nbsp; &nbsp; int player_y;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; char test[102];<br />
&nbsp; &nbsp; int test_frame = 0;<br />
&nbsp; &nbsp; char *test2;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; pthread_t threads[MAX_THREADS];<br />
&nbsp; &nbsp; int i = 0;<br />
&nbsp; &nbsp; //i is used for determining how many players have connected.<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; while (gamerunning) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connect = accept(Listen, (SOCKADDR*)&amp;Server, &amp;size);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Accept the client<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i &gt;= 0 &amp;&amp; i &lt; MAX_THREADS) { //Tell that the client cannot connect passed the set amount<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  rc = pthread_create(&amp;threads[i], NULL, ClientThread, (void *)i);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  std::cout &lt;&lt; &quot;Threads: &quot; &lt;&lt; i &lt;&lt; &quot;\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  i +=1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Connect != NULL) { //If the connection was successful<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  std::cout &lt;&lt; &quot;Connection Accepted\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  srvr_connect = true;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (srvr_connect == true) { //If the server is connected to the internet<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (test_frame == 0) { //Do this only once (Since the '0' changes later)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  std::cout &lt;&lt; &quot;Connection Sent!\nConnection Has Been Breached!\nPlayers Are Now Able to Join Your Server!\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  send(Connect, hey, sizeof(hey), 0);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  std::cout &lt;&lt; &quot;\n\nSent Data: &quot; &lt;&lt; hey &lt;&lt; &quot; (This is strictly for binding connections)\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  test_frame +=1; //Stops the loop of this (as said above)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recv(Connect, test, sizeof(test), 0);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::cout &lt;&lt; test &lt;&lt; &quot;\n&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; std::cin.get();<br />
&nbsp; &nbsp; return 0;<br />
}</code><hr />
</div> <br />
I had set notes by things to tell what they do! <br />
Hopefully I can get some more info on how to do this.<br />
 <br />
Maybe fix up my code? xP<br />
 Thanks, Guys!<br />
 I will try my best on understanding most of your replies on this.<br />
 <br />
-LunarB <br />
<br />
P.S: Feel free to give me constructive critisism.<br />
I'm not entirely sure what's wrong with my code, <br />
As i'm no expert (Or close) with networking multiple clients.</div>

 ]]></content:encoded>
			<category domain="http://forums.codeguru.com/forumdisplay.php?62-Network-Programming">Network Programming</category>
			<dc:creator>LunarB</dc:creator>
			<guid isPermaLink="true">http://forums.codeguru.com/showthread.php?536807-Winsock2-amp-pthreads-Accepting-Multiple-Clients</guid>
		</item>
		<item>
			<title>multiplyer game ....</title>
			<link>http://forums.codeguru.com/showthread.php?536529-multiplyer-game-....&amp;goto=newpost</link>
			<pubDate>Thu, 25 Apr 2013 19:58:26 GMT</pubDate>
			<description>hey,
    i made  a game on c++ using allegro now i want to make it multiplayer game means connect pair to pair connection with another pc and enjoy the game......how i develop this connection please help me 
help me with code .......please help urgent</description>
			<content:encoded><![CDATA[<div>hey,<br />
    i made  a game on c++ using allegro now i want to make it multiplayer game means connect pair to pair connection with another pc and enjoy the game......how i develop this connection please help me <br />
help me with code .......please help urgent</div>

 ]]></content:encoded>
			<category domain="http://forums.codeguru.com/forumdisplay.php?62-Network-Programming">Network Programming</category>
			<dc:creator>karan124</dc:creator>
			<guid isPermaLink="true">http://forums.codeguru.com/showthread.php?536529-multiplyer-game-....</guid>
		</item>
	</channel>
</rss>
