Register for your free account! | Forgot your password?

You last visited: Today at 11:50

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Sockets

Discussion on Sockets within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Sockets

well i've Gone through socket and understood lots of things ..i got questions about it .. i've read that Async Sockets are better than Sync socket cuz Sync sockets Blocks the main Thread unlike the Async so the question is .. since Async is better than Sync socket why they have created it since every1 would use the better socket Type which is Async ... in other Rephrase when should i use Sync sockets ? .. and i'd like to know about Cons and Pros of using TcpListeners and TcpClient instead of Mentioned Socket Types above ...

Another question i've created Windows Form Application which is Server but the problem is that when i create this code the Application Freezes and when i click on it ..it gives me Not Responding Error ..Same for client ... it connects to the server then Freeze and give me same Not Responding Error

Problem ScreenShot

Here Are the Codes I used
used Ip = "127.0.0.1", Port = 5100
Code:
public void StartServer(string Ip, ushort Port)
        {
            Soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Soc.Bind(new IPEndPoint(IPAddress.Parse(Ip), Port));
            textBox2.Text += " Server Is Created On Ip : " + IpAdd + ", Port : " + Port + Environment.NewLine;
            Soc.Listen(100);
            textBox2.Text += " Server Is Listening to Clients Now... " + Environment.NewLine;
            StartListening();
        }
Code:
public void StartListening()
        {
            while (true)
            {
                var Client = Soc.Accept();
                textBox2.Text += " Client Located At Ip : " + Client.RemoteEndPoint + " Is Now Connected To Server " + Environment.NewLine;
                byte[] ReceivedBytes = new byte[1024];
                int ReceivedDataLength = 0;
                while ((ReceivedDataLength = Soc.Receive(ReceivedBytes)) > 0)
                {
                    string MessageFromClient = ASCIIEncoding.ASCII.GetString(ReceivedBytes, 0, ReceivedBytes.Length);
                    textBox2.Text += " Message Sent to Server : " + MessageFromClient + Environment.NewLine;
                }
            }
        }
Additional Question : How can i remove that Stupid Shadow surrounding the Application Border ? its makes the Application look worse , shitty

regards
shadowman123
shadowman123 is offline  
Old 09/25/2013, 06:10   #2
 
TheComputerist's Avatar
 
elite*gold: 0
Join Date: Jun 2012
Posts: 21
Received Thanks: 4
1: Blocking sockets where used in prehistoric times when dinosaurs ruled the internet and didn't have a better programming scheme/ didn't have computers strong enough to do such things. So it's there for anyone that still wants it, kind of.

2: There are multiple answers, it could be due to bad code, an infinite loop that is never exited, etc. Don't go straight for "Portforward" or "Opening the port for firewall" because that doesn't freeze the window. The only thing it should do is not let you connect to the server at all therefore giving you an error. But lets say you ignore that error and try to connect, you might have a loop somewhere that causes the freeze due to the bad socket/ connection.

3: I don't know of anyway to remove the black border. Wait now that I think about it. You could create a windows class without borders and have Direct2d or GDI(Depending on your windows version) to draw to the window without having that border(I haven't tried this so take it with a grain of salt).
TheComputerist is offline  
Thanks
1 User
Old 09/25/2013, 07:11   #3
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Thx Alot dude for your Answer ... Helped me Alot but for the sockets part ... i use while (true) 2 Times .. 1st is for listening to incoming client connection , 2nd for Receiving Data so u think thats the problem ? even when i use Thread.Sleep method i get the same result ... idk what to do but im stuck really at this point . i'd like to cross this **** to learn smthing new :S
shadowman123 is offline  
Old 09/25/2013, 14:04   #4
 
TheComputerist's Avatar
 
elite*gold: 0
Join Date: Jun 2012
Posts: 21
Received Thanks: 4
Hmm well I can't make out the error from that description but just double check your code to see if it follow a similar schematics to this.

Server:
1:Create socket (Set it up obviously)
2:Bind socket to a local port, in your case it's 127.0.0.1 port 5000 which is a localhost address.
3:Put that binded socket into listening mode.(Once a socket is in listening mode you can reuse it to accept multiple connections given that you don't overwritte the listening socket variables value with something else)
4: You could rather go two easy ways. Accept only one connection and have a P2P connection going on or have it be dynamic in the sense that you have the main thread always Accepting new connections to new sockets. Then you have a separate thread handling sending and receiving messages(The operating system should handle synchronization in this case).
5: Which ever way you go it's almost the same. Accept a connection into a separate socket than the one that's listening. Send some data to client. And have the client receive it. Visa versa.

Client:
1:Create socket (Set it up obviously)
2:Bind socket to a local port, 127.0.0.1 but with a different port that's not 5000 since your server will be using that port and you'll get an error if you try to bind to an already binded port
3: Settup the correct parameters(IP:127.0.0.1, Port:5000) for the socket you want to connect to.
4:Attempt to connect to the server and recv data or send.

Ps. Remember to give your processor some breathing room for now. So (I think it's the same for C# as in c++) use the function Sleep at the end of the loops that send and recv data just for now.

BONUS: Look what I found on google about borderless windows / the answer to your prayers
This is the best one i've found on getting rid of boders... Given it's c++ but it remains.



These use WPF which is a .NET thing...
TheComputerist is offline  
Old 09/25/2013, 18:21   #5
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Quote:
Originally Posted by TheComputerist View Post

Client:
1:Create socket (Set it up obviously)
2:Bind socket to a local port, 127.0.0.1 but with a different port that's not 5000 since your server will be using that port and you'll get an error if you try to bind to an already binded port
3: Settup the correct parameters(IP:127.0.0.1, Port:5000) for the socket you want to connect to.
4:Attempt to connect to the server and recv data or send.

Ps. Remember to give your processor some breathing room for now. So (I think it's the same for C# as in c++) use the function Sleep at the end of the loops that send and recv data just for now.
y would i bind the client to speific Port since all client do is connect ???
im talking about Win Form Application not WPF .. i dislike XAML language beside i dont understand anything with it anyways u r alawys helping me and thats smthing i really Appriciate .. Thx dude
shadowman123 is offline  
Old 09/25/2013, 21:13   #6
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
i get the same Result Yuki event if i did .. Thread.Sleep(1000000000); i tried everything
shadowman123 is offline  
Old 09/25/2013, 22:19   #7
 
TheComputerist's Avatar
 
elite*gold: 0
Join Date: Jun 2012
Posts: 21
Received Thanks: 4
Quote:
Originally Posted by shadowman123 View Post
y would i bind the client to speific Port since all client do is connect ???
im talking about Win Form Application not WPF .. i dislike XAML language beside i dont understand anything with it anyways u r alawys helping me and thats smthing i really Appriciate .. Thx dude
Ups, yeah you're right, you don't have to bind when you're dealing with a client that is connecting lol. Although I've used the bind + connect without any problem.Sorry. So any luck with your problem, both borders and freezing?
TheComputerist is offline  
Old 09/26/2013, 00:34   #8
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Quote:
Originally Posted by TheComputerist View Post
Ups, yeah you're right, you don't have to bind when you're dealing with a client that is connecting lol. Although I've used the bind + connect without any problem.Sorry. So any luck with your problem, both borders and freezing?
well i Wrote my Problem in Stackflow site and received this Code .. it works but for Send it gives me error saying that threre is no Address Ip selected ...

the code after their Edit became

Code:
public void ReceiveData()
        {
            new Thread(() =>
            {
                while (true)
                {
                    var Client = Soc.Accept();
                    ClientList.Add(Client);
                    label3.Text = ClientList.Count.ToString() + "/" + MaxClients;
                    textBox2.Text += " Client Located At Ip : " + Client.RemoteEndPoint.ToString().Split(':')[0].ToString() + " Is Now Connected To Server " + Environment.NewLine;

                    new Thread(() =>
                    {
                        while (true)
                        {
                            byte[] ReceivedBytes = new byte[1024];
                            int ReceivedDataLength = Client.Receive(ReceivedBytes, 0, ReceivedBytes.Length, SocketFlags.None);
                            string MessageFromClient = Encoding.Default.GetString(ReceivedBytes, 0, ReceivedDataLength);

                        }
                    }).Start();
                }
            }).Start();
        }
Sending Message From Client to Server is okay but when From Server -> client it give me that Error



Sending Code From Server
Code:
private void button1_Click(object sender, EventArgs e) // Send
        {
            try
            {
                string MessageToSend = textBox1.Text;
                byte[] Buffer = Encoding.Default.GetBytes(MessageToSend);
                foreach (var client in ClientList)
                {
                    ServerSocket.Send(Buffer, 0, Buffer.Length, SocketFlags.None);
                }
                textBox2.Text += " Server : " + MessageToSend + Environment.NewLine;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }
shadowman123 is offline  
Old 09/26/2013, 07:50   #9
 
TheComputerist's Avatar
 
elite*gold: 0
Join Date: Jun 2012
Posts: 21
Received Thanks: 4
When dealing with UDP packets it's required to bind. Not so much on TCP. And this has gone on long enough, someone who knows C# better than me needs to finish this lol.
But I'm still trying to figure this one out @_@.
TheComputerist is offline  
Old 09/26/2013, 08:45   #10
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Quote:
Originally Posted by TheComputerist View Post
When dealing with UDP packets it's required to bind. Not so much on TCP. And this has gone on long enough, someone who knows C# better than me needs to finish this lol.
But I'm still trying to figure this one out @_@.
you know ..i can use Async socket and its gonna work but i would like / Insist to know whats wrong causing this ... i spent long time trying to understand but havent got result yet
shadowman123 is offline  
Old 09/29/2013, 14:46   #11
 
elite*gold: 0
Join Date: Jun 2007
Posts: 96
Received Thanks: 12
make a new thread for the whole socket not only the receive loop, and don't sleep the main thread that will cause application freeze.
atef201080 is offline  
Old 09/30/2013, 11:19   #12
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Quote:
Originally Posted by atef201080 View Post
make a new thread for the whole socket not only the receive loop, and don't sleep the main thread that will cause application freeze.
no u are completely Wrong , if you didnt Call Thread.Sleep() Method the Application is going to raise the CPU Alot .. so u should call this method ( Thats y they created it ) .. beside it wont affect badly on Project if you used suitable sleep time lets say > 0 && < 1000 would be Fine ..if more than 1000 app might have problems with Application Function

and about the Threading .. yea i should add all Socket system Methods within new Thread in order not to block the main Thread from working but anyways ill try to figure out whats wrong but really Async is much better and very easy to be Implemented
shadowman123 is offline  
Old 09/30/2013, 17:22   #13
 
elite*gold: 0
Join Date: Jun 2007
Posts: 96
Received Thanks: 12
Quote:
Originally Posted by shadowman123 View Post
no u are completely Wrong , if you didnt Call Thread.Sleep() Method the Application is going to raise the CPU Alot .. so u should call this method ( Thats y they created it ) .. beside it wont affect badly on Project if you used suitable sleep time lets say > 0 && < 1000 would be Fine ..if more than 1000 app might have problems with Application Function

and about the Threading .. yea i should add all Socket system Methods within new Thread in order not to block the main Thread from working but anyways ill try to figure out whats wrong but really Async is much better and very easy to be Implemented
if you put a thread sleep on the main thread the form will freeze while the sleep time, just put the socket and everything in a void and start it using new thread
PHP Code:
SocketSystem = new Thread(() => SocketSystem(parametrs));
SocketSystem.Start(); 
atef201080 is offline  
Reply


Similar Threads Similar Threads
Q>Sockets
01/05/2012 - Grand Chase Philippines - 5 Replies
Is it possible to get 3-4 socket? and What does HP Drain do?
Sockets first to log in after SM
03/14/2007 - Conquer Online 2 - 13 Replies
If i was the first to log in after SM will the 1st met be the socket? or is there a certian amount i would have to spam? Thanks :) cheers i have quit this game for a while but I am back now. :P
sockets
02/05/2007 - Conquer Online 2 - 3 Replies
have here some of my socket triks and hints 1. time is always changing when the server patched so just type you message at the message board then download some programs that display the time. 2. i made sockets with different location and i think coordinate does'nt matter maybe time matters. 3. time possible to make sockets is at xx:00-xx:05, xx:10 - 15, xx: 30 - 35, xx:40 - 45 with me. real time clock come from my computer. 4. GM message always pops up every 20mins
About sockets...
01/03/2007 - Conquer Online 2 - 6 Replies
Hi people. 1 my friend told me that it is possibility to make socket, when there is server maintaince. He said that after maintaince i have to try few times to make sockets. I dont know if its really trustfull so i decided to ask you all. Can anyone please answer me if its true, and if it is, how to make sockets correctly. Thanks



All times are GMT +1. The time now is 11:51.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.