Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 16:03

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

Advertisement



Socket/Threading Question

Discussion on Socket/Threading Question within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
xBlackPlagu3x's Avatar
 
elite*gold: 0
Join Date: Jan 2011
Posts: 286
Received Thanks: 71
Exclamation Socket/Threading Question

Alright so I've been following InfamousNoone's C# for idiots tutorial (video set) and I've just finished watching them all. I can now say I'm very very interested in sockets and whatnot. So the problem is, I'm trying to use sockets to creating a mini Messaging client and I've gotten to the point where I can send a message back in forth. But this is how it works.

The ONLY way you can send a message is if you receive one first. That's obviously a problem! I've tried to start multiple threads but I just don't know how. (I just got out of a threading lesson but 5 days ago) No matter how I do it, I can't figure out a way to be able to send messages over and over without having a block. Can anyone tell me how I'd be able to do this?


Here's the entire code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.NativeInterop;
using System.Net.Sockets;
using System.Threading;

namespace MeteoraMSGClient
{
    class Program
    {
        public static string MSG;
        private static void MSGSendThread(WinsockClient Sender, object Arg)
        {
            MSG = Console.ReadLine();
            Sender.Send(Encoding.ASCII.GetBytes(MSG));
        }
        static void Main(string[] args)
        {

            WinsockClient Client = new WinsockClient(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Client.OnConnect += new SocketEventCallback<WinsockClient, object>(Client_OnConnect);
            Client.OnDisconnect += new SocketEventCallback<WinsockClient, object>(Client_OnDisconnect);
            Client.OnReceive += new SocketEventCallback<WinsockClient, byte[]>(Client_OnReceive);

            byte[] ClientRecvBuffer = new byte[255];
            Client.Enable("**********", 9958, ClientRecvBuffer);
                
        }
        static void Client_OnReceive(WinsockClient Sender, byte[] Arg)
        {
            Console.WriteLine("New message!");
            Console.WriteLine("\t >> " + Encoding.ASCII.GetString(Arg));
            MSG = Console.ReadLine();
            Sender.Send(Encoding.ASCII.GetBytes(MSG));
        }

        static void Client_OnDisconnect(WinsockClient Sender, object Arg)
        {
            Console.WriteLine("We have disconnected from the server");
        }

        static void Client_OnConnect(WinsockClient Sender, object Arg)
        {
            Console.WriteLine("We have connected to a server");
            MSG = Console.ReadLine();
            Sender.Send(Encoding.ASCII.GetBytes(MSG));
        }
    }
}
xBlackPlagu3x is offline  
Old 07/31/2011, 23:06   #2
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,376
You need to keep an instance of the WinsockClient so you can use its .Send method.

This could be done via a dictionary, a single 'user' or however you want.

When someone connects you save that connection and then use it to .Send.

Example.

WinsockClient user;

in the on connect you say

user = Sender;

then whenever you want to send something just do user.Send(byte array);
pro4never is offline  
Old 07/31/2011, 23:10   #3
 
xBlackPlagu3x's Avatar
 
elite*gold: 0
Join Date: Jan 2011
Posts: 286
Received Thanks: 71
Quote:
Originally Posted by pro4never View Post
You need to keep an instance of the WinsockClient so you can use its .Send method.

This could be done via a dictionary, a single 'user' or however you want.

When someone connects you save that connection and then use it to .Send.

Example.

WinsockClient user;

in the on connect you say

user = Sender;

then whenever you want to send something just do user.Send(byte array);
(First off, I don't know if you've just become one but I've never noticed it before so congratulations on becoming a mod)

This sounds exactly what I'd be needing to use but could you either reword it or add a little bit more to your example? I've never used a dictionary entry or hashtable, etc.

Also, basically what you're saying is that I could handle the messaging in the OnConnect?
xBlackPlagu3x is offline  
Old 07/31/2011, 23:17   #4
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,376
Yes, you'd handle it during OnConnect.

a Dictionary lets you hold a key and value as a pair. This means that you could (for example) do something like...


Dictionary<WinsockClient, Player> Clients = new Dictionary<WinsockClient, Player>();


Then during the OnConnect you'd say...

if(!Clients.ContainsKey(Sender))
Clients.Add(Sender, new Player(Sender));

On Disconnect you'd say...

if(Clients.ContainsKey(Sender))
Clients.Remove(Sender);

On receive you'd say...

if(!Clients.ContainsKey(Sender))
//Throw error

else you know which Player has sent you a packet and you can process the packet for that user.


There are many ways of doing it many of which have no uses for dictionaries, this is just a simple example.
pro4never is offline  
Reply


Similar Threads Similar Threads
Question|Bot Socket
04/05/2011 - SRO Coding Corner - 10 Replies
ok soo today i decide to work about a new bot thet based on vb6 lang soo i want to ask about a little help in script make the bot work like clientless like (sbot) or (sroking) if game crashed or you try to connect directly via the bot i will be glad for any help thank for helpers ;)
My Socket Wrapper With Multi Threading and Events Similar to Classic Winsock
10/25/2010 - CO2 Programming - 9 Replies
Hi guys to those who are planning to make their own proxy and want to have a socket wrapper which is similar to classic winsock then this class below is the one you are looking. I created a multi threading socket of System.Net.Sockets. I also created the events similar to classic winsock such as OnConnectionRequest, OnDisconnection, OnConnect, OnDataArrival, OnSendComplete and OnError. Imports System.Net.Sockets Imports System.Net Imports System.Text Imports System.Threading
Socket Question....
03/16/2009 - Conquer Online 2 - 2 Replies
Hey. this is gunna sound really nub but here it goes... Can you find socketed non-weapon equipment on the ground? i've never found one >< i tried to :rtfm: on thier website but saw nothing about it. Thanks!
A Socket question
09/06/2007 - Conquer Online 2 - 3 Replies
Hi Well I just got a lvl 15 +2 super coat with sock and I was wondering that if there are some "techniqs" to have high chance on getting a double sock in tc spamming mets, like if there is a difrence between spamming a +2 or +3 item, etc..or its just pure luck? tks :)



All times are GMT +2. The time now is 16:03.


Powered by vBulletin®
Copyright ©2000 - 2024, 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 ©2024 elitepvpers All Rights Reserved.