create a connection client

Started by
13 comments, last by Pedro Alves 10 years, 8 months ago
i can create a connection to the server with neoforce project this is the code
the problem when the user make login with sucess add the user to the server
the same code with form works fine with neoforce i have this errors and when the user send a text in send a name of the user and not a time
Error 4 The name 'Invoke' does not exist in the current context C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 161 13 Central
Error 1 'TomShane.Neoforce.Controls.Layout': static types cannot be used as return types C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 38 24 Central
Error 2 'TomShane.Neoforce.Controls.Layout': static types cannot be used as parameters C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 38 29 Central
Error 3 'TomShane.Neoforce.Controls.Layout': static types cannot be used as parameters C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 41 17 Central
Error 5 'TomShane.Neoforce.Controls.Layout' does not contain a definition for 'Invoke' and no extension method 'Invoke' accepting a first argument of type 'TomShane.Neoforce.Controls.Layout' could be found (are you missing a using directive or an assembly reference?) C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 171 21 Central
Error 6 'TomShane.Neoforce.Controls.Layout' does not contain a definition for 'Invoke' and no extension method 'Invoke' accepting a first argument of type 'TomShane.Neoforce.Controls.Layout' could be found (are you missing a using directive or an assembly reference?) C:\Users\Alves\Dropbox\Jogos\TomShane.Neoforce.Controls.XNA4\Neoforce\Source\Central\Code\World.cs 181 21 Central
class world.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Shared.Protocol;
using Shared.User;
using Message = Shared.Messages.Message;
using System.Windows.Forms;
using FableServer;
using TomShane.Neoforce.Central;

namespace TomShane.Neoforce.Central
{
public sealed partial class World : Window
{
#region Fields
const int Port = 50143;
delegate void SendTextDelegate(Message message);
delegate void AddUserDelegate(User user);
delegate void ClearUserListDelegate();
#endregion
#region Properties
User User { get; set; }
TcpClient TcpClient { get; set; }
BinaryWriter Writer { get; set; }
BinaryReader Reader { get; set; }
Layout Instance { get; set; }
#endregion

public World(Manager manager,string text, Layout instance): base(manager)
{
User = new User(text, System.Drawing.Color.Black, Rank.Normal);
Instance = instance;
SetupClient();

InitConsole();

}


void SetupClient()
{
try
{
TcpClient = new TcpClient("localhost", Port);
Writer = new BinaryWriter(new NetworkStream(TcpClient.Client));
Reader = new BinaryReader(new NetworkStream(TcpClient.Client));
ThreadStart ts = delegate
{
HandleServer();
Close();
};
var t = new Thread(ts);
t.Start();
//Setup Username and send it to server
// new Message("Ola: " + User.Username, new User("Server", Color.Yellow, Rank.Admin));
Send(PacketType.SendUser, User);
GetUserList();
}
catch (Exception e)
{
TomShane.Neoforce.Controls.MessageBox.Show(Manager, TomShane.Neoforce.Controls.MessageBox.MessageBoxType.CANCEL, "ERRO"+e, "");
CloseServer();
}
}
#region Methods

void GetUserList()
{
//Get list of users on the server
//AppendText(new Message("WELCOME TO ", new User("Server", Color.Gold, Rank.Admin)));
// AppendText(new Message("WELCOME TO SERVER: " + User.Username + "", new User("Server", Color.Gold, Rank.Admin)));
Send(PacketType.SendUserList, null);
}
void CloseServer()
{
if (Writer != null) Writer.Close();
if (Reader != null) Reader.Close();
if (TcpClient != null) TcpClient.Close();
//Logon.Exit();
}
void HandleServer()
{
try
{
ProtocolObject packet;
while ((packet = Recieve()) != null)
{
switch (packet.PacketType)
{
#region RecieveChatMessage
case PacketType.RecieveChatMessage:
{
InvokeLog((Message)packet.Payload);
}
break;
#endregion
#region RecieveUserList
case PacketType.RecieveUserList:
{
InvokeUserList();
foreach (var l in (List<User>)packet.Payload)
InvokeUser(l);
}
break;
#endregion
}
}
}
catch (Exception e)
{
TomShane.Neoforce.Controls.MessageBox.Show(Manager, TomShane.Neoforce.Controls.MessageBox.MessageBoxType.CANCEL, "ERRO"+e, "");
CloseServer();
}
}

void Title(Message message)
{
AppendText(message);
Text = message.Text == null ? @"Client" : @"Client - " + message.Text;
}
#endregion
#region Send and Recieve
void Send(PacketType packetType, object payload)
{
DataProtocolObject.SerializePacket(Writer, new ProtocolObject(packetType, payload));
}
ProtocolObject Recieve()
{
return DataProtocolObject.DeserializePacket(Reader);
}
#endregion
#region Delegates
void InvokeLog(Message message)
{
Invoke(new SendTextDelegate(SendText), message);
}
void SendText(Message message)
{
AppendText(message);
}
void InvokeUser(User user)
{
Instance.Invoke(new AddUserDelegate(AddUser), user);
}

void AddUser(User user)
{
// AddUserToList(user);
}
void InvokeUserList()
{
Instance.Invoke(new ClearUserListDelegate(ClearUsers));
}
void ClearUsers()
{
// userList.Items.Clear();
}
#endregion
void AppendText(Message message)
{
//con1.SelectionColor = message.User.Color;
//con1.AppendText(Environment.NewLine + message.User.Username + ": " + message.Text);
}

#region Console
private void InitConsole()
{
TomShane.Neoforce.Controls.TabControl tbc = new TomShane.Neoforce.Controls.TabControl(Manager);
TomShane.Neoforce.Controls.Console con1 = new TomShane.Neoforce.Controls.Console(Manager);
TomShane.Neoforce.Controls.Console con2 = new TomShane.Neoforce.Controls.Console(Manager);
//Console con3 = new Console(Manager);

// Setup of TabControl, which will be holding both consoles
tbc.Init();
tbc.AddPage("Global");
tbc.AddPage("Guild");
tbc.AddPage("PARTY");
tbc.AddPage("TRADE");
tbc.Alpha = 220;
tbc.Left = 220;
tbc.Height = 220;
tbc.Width = 400;
tbc.Top = Manager.TargetHeight - tbc.Height - 32;

tbc.Movable = true;
tbc.Resizable = true;
tbc.MinimumHeight = 96;
tbc.MinimumWidth = 160;

tbc.TabPages[0].Add(con1);
tbc.TabPages[1].Add(con2);
//tbc.TabPages[2].Add(con3);

con1.Init();
con2.Init();
//con3.Init();
con2.Width = con1.Width = tbc.TabPages[0].ClientWidth;
con2.Height = con1.Height = tbc.TabPages[0].ClientHeight;
con2.Anchor = con1.Anchor = Anchors.All;
con1.Channels.Add(new ConsoleChannel(0, "General", Color.Orange));
con1.Channels.Add(new ConsoleChannel(1, "Private", Color.White));
con1.Channels.Add(new ConsoleChannel(2, "System", Color.Yellow));
con1.Channels.Add(new ConsoleChannel(3, "Guild", Color.Green));

// We want to share channels and message buffer in both consoles
con2.Channels = con1.Channels;
con2.MessageBuffer = con1.MessageBuffer;

// In the second console we display only "Private" messages
con2.ChannelFilter.Add(3);

// Select default channels for each tab
con1.SelectedChannel = 0;
con2.SelectedChannel = 3;
//con3.SelectedChannel = 3;

// Do we want to add timestamp or channel name at the start of every message?
con1.MessageFormat = ConsoleMessageFormats.All;
con2.MessageFormat = ConsoleMessageFormats.TimeStamp;
// Handler for altering incoming message
con1.MessageSent += new ConsoleMessageEventHandler(con1_MessageSent);

// We send initial welcome message to System channel
con1.MessageBuffer.Add(new ConsoleMessage("WELCOME TO THE SERVER!",2));

Manager.Add(tbc);
}
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
void con1_MessageSent(object sender, ConsoleMessageEventArgs e)
{
if (e.Message.Channel == 0)
{
//e.Message.Text = "(!) " + e.Message.Text;
}
}
////////////////////////////////////////////////////////////////////////////

#endregion

}
}


class logic.cs

void btnApply_Click(object sender, Controls.EventArgs e)
{
String user = txusername.Text.Trim();
System.Console.WriteLine(user);
String userPassword = txpassword.Text.Trim();
System.Console.WriteLine(userPassword);
try
{

MySqlConnection con = new MySqlConnection("host=localhost;user=root;database=jogo");
MySqlCommand cmd = new MySqlCommand("SELECT password FROM w70tg_users WHERE username ='" + user + "'");
cmd.Connection = con;
con.Open();
object hello = cmd.ExecuteScalar();
if (hello == null)
{
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL, "ERRO", "");
}
else
{
string password = hello.ToString();
// que hizo el usuario escribe?
con.Close();
if (Check(userPassword, password))
{
// corregir!
MySqlCommand cmdo = new MySqlCommand("SELECT w70tg_user_usergroup_map.group_id FROM w70tg_users INNER JOIN w70tg_user_usergroup_map on w70tg_user_usergroup_map.user_id=w70tg_users.id WHERE username = '" + txusername.Text + "'");
cmdo.Connection = con;
con.Open();
object Tipo = cmdo.ExecuteScalar();
if (Tipo != null && Tipo != DBNull.Value)
{
switch (System.Convert.ToInt32(Tipo))
{
case 8:
// serverselection ola = new serverselection();
// ola.Show();
// Hide();
World ola = new World(Manager);

ola.Init();

ola.Width = 300;
ola.Height = 225;
ola.Center();
ola.Visible = true;
txusername.Hide();
txpassword.Hide();
lbusername.Hide();
lbpassword.Hide();
btnApply.Hide();
btnExit.Hide();
ola.CloseButtonVisible = false;

break;
case 11:
MessageBox.Show( Manager,MessageBox.MessageBoxType.CANCEL,"GAME MASTER","");
break;
case 10:
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL, "Moderador","");
break;
case 3:
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL, "VIP", "");
break;
case 2:
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL, "Membro", "");
break;
case 1:
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL, "O Utilizador foi banido\n Contacte a Equipa atravez do suporte para saber a razão pelo qual foi banido(a)","");
break;
}
}
}
else
{

txpassword.Text = null;
txusername.Text = null;
}
con.Close();
}
}
catch (MySqlException msqle)
{
MessageBox.Show(Manager, MessageBox.MessageBoxType.CANCEL,"ERRO"+msqle.Message, "");
}

}

Hello

Advertisement
Since the question appears to be only incidentally concerning networking, I have moved it to general programming.
static types cannot be used as return types [/quote]

This is a C# error message about the code syntax.
It looks like the "Neoforce" library is not designed to work in the environment you're trying to use it.
enum Bool { True, False, FileNotFound };
i want connect the neoforce project to my server but i can´t
how i put the neoforce library work with tcp

Hello

how i solve my problem
someone can help me

Hello

The errors you are getting point to fundamental weaknesses in your current understanding of the language. This indicates that your chosen project is well above the level of your abilities at this time. This is the programming equivalent of trying to run before you can walk.

I would recommend you put this project on hold for the time being, and go back to learning the core language. Buy a good book, or borrow one from your local library, and go through it, paying particular attention to completing all the exercises at the end of each chapter.

Once you've built up your skills, then you can try to tackle a few small games first. Building games like this will get your learning about how to design larger and larger programs, which is another skill that you'll need. Designing a simple game like Pong is quite different from something like Mario or Doom.

I wouldn't recommend trying a networked game as one of your first few projects, there are a lot of skills you will need to be able to make such a game. Your first networked program should probably be something like a chat client or a simple HTTP server, something to familiarise yourself with the concepts and how to debug issues.

This is true for all technologies. If you aren't comfortable using a database make a separate project to get familiar - for example implement some simple DVD or Book cataloguing software. If you have never made a game using Neoforce then write something small using it to ensure you understand what it gives you and how to use it.

Finally, when you've built up your core language skills, your project design skills and your skills in the specific technologies you want to use, then return to your current project.
making network connection with form i can make but with neoforce not
becouse the mettod invoke don´t exist in neoforce project
that is my problem

Hello

I understand your problem. The code will not be the same in both cases. The fact that you appear to expect it to be, and the level of technical detail in your reply indicates you have not yet got all the necessary skills I have been talking about.

If you have been copying code from tutorials, be aware that this can make you appear to be able to do more than you actually can. Part of learning a new skill is understanding how much or little one knows - the ability to making accurate introspective judgements. In my experience as a developer, and as someone who has seen people at all levels of skill here on gamedev.net, I am fairly certain of my evaluation of your current ability.

If you can tell me what you have tried in enough detail to prove my evaluation wrong (for example, perhaps the language barrier is interfering with my perception), I will certainly try to help you. But I can not meaningfully help you with this specific problem right now, because the real cause of this problem is you being out of your depth.
what is the name dll file is responsible for the method of invoke
if a add this file to my references maybe solve my problem

Hello

This topic is closed to new replies.

Advertisement