|
I am doing a TCP server & client communication. I would like to display the received data (from client) on the textbox of the server.
When i used txtStatus.AppendText, it works perfectly fine. But when i changed to txtStatus.Text, nothing was shown in the textbox.
I have tried changing to
txtStatus.Text += recMovementData.BaseMoveData.BaseMov.ToString();
but to no avail
It should work as it is the same as .AppendText but it doesnt.
Can anyone help me with this?
This is my code for my server:
private void btnListen_Click(object sender, EventArgs e)
{
//IPAddress ipaddress = IPAddress.Parse(txtIP1.Text + "." + txtIP2.Text + "." + txtIP3.Text + "." + txtIP4.Text);
//int portNum = Convert.ToInt32(txtPort.Text);
IPAddress ipaddress = new IPAddress(new byte[] { 123, 123, 123, 123 });
int portNum = 8500;
TCPServerHelper.AcceptConnection(ipaddress, portNum);
txtStatus.Text = "Connected to remote client!";
while (true)
{
byte[] bufferReceivedData = TCPServerHelper.Read();
MovementData recMovementData = (MovementData)TCPServerHelper.Deserialize(bufferReceivedData);
try
{
//txtStatus.AppendText(recMovementData.BaseMoveData.BaseMov.ToString());
txtStatus.Text += recMovementData.BaseMoveData.BaseMov.ToString();
}
catch
{ }
}
|
|
|
|
|
Are you sure that the += operator is overridden for this property. I notice that you have an empty catch block in your code, which may well be why you don't see anything. There is no point using try/catch if you just ignore any exceptions.
|
|
|
|
|
I have an SQLite database with a column type of BLOB. But, here I inserted a text, because is a very long text and data types TEXT,STRING or VARCHAR does not support such a length. How can I show the text in a richTextBox? The code so far:
using (Conexiune.getConnection())
{
string select = "SELECT * FROM legislatie WHERE capitol = '" + SimulatorManager.LegislatieCapitol + "'";
SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection());
cmd.CommandType = CommandType.Text;
SQLiteDataReader rdra = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
while (rdra.Read())
{
data = (byte[])(rdra["date"]);
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message);
}
MemoryStream ms = new MemoryStream(data);
richTextBox1.Text = data.ToString();
}
|
|
|
|
|
Your code is vulnerable to SQL Injection[^].
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
You're calling Conexiune.getConnection() twice, throwing away the result of the first call, and not disposing of the result of the second call.
You should wrap your disposable objects (the connection, command, and datareader) in using blocks.
You shouldn't use SELECT * FROM - state the column(s) you want to load explicitly.
There's no need to use a datareader to select a single value. ExecuteScalar will return the first column of the first row of the query results. You can also add a LIMIT 1 at the end of the query, since you're only looking at a single record.
To convert a byte[] array to a string, you'll probably need to the Encoding class[^]. Which encoding you use will depend on how your data was added to the database.
const string select = "SELECT data FROM legislatie WHERE capitol = @capitol LIMIT 1";
using (SqlLiteConnection connection = Conexiune.getConnection())
using (SqlLiteCommand cmd = new SqlListCommand(select, connection))
{
cmd.Parameters.AddWithValue("@capitol", SimulatorManager.LegislatieCapitol);
cmd.CommandType = CommandType.Text;
var data = cmd.ExecuteScalar() as byte[];
if (data == null || data.Length == 0)
{
richTextBox1.Text = string.Empty;
}
else
{
richTextBox1.Text = System.Text.Encoding.Default.GetString(data);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I see, thank you, it worked
|
|
|
|
|
Hi everybody, i would like some help i am studying c#, however the text book i am studying from has examples that doesnt make any sense. Can someone please give me a example of how to use property in consuming classes. Thank you very much.
|
|
|
|
|
I'm not too sure what your actual problem is. Does the following help?
class ClassWithProperties
{
public string SomeProperty { get; private set; }
public ClassWithProperties(string someProperty)
{
SomeProperty = someProperty;
}
}
class ConsumingClass
{
public string SomeOtherProperty { get; private set; }
public ConsumingClass(ClassWithProperties cwp)
{
SomeOtherProperty = cwp.SomeProperty + "foo";
}
}
If not, please clarify.
Here's a good and free PDF-book on programming in C#: http://www.introprogramming.info/[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
hi to all and sorry for my bad english (im Italian).
i have a code (c#) for chat server and client using NetworksApi library. server and client works good but only in local (192.168.1.--).
what i would is the same operation but with public Ip (internet for far PC).
I tried putting the public IP and port number but nothing works.
here is my code for SERVER :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.SERVER;
using System.IO;
using System.Net;
namespace AdvanceServer
{
public delegate void UpdateChatLog(string txt);
public delegate void UpdateListBox(ListBox box,string value, bool Remove);
public partial class Form1 : Form
{
Server server;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
server = new Server("192.168.1.6","90");
server.OnClientConnected += new OnConnectedDelegate(server_OnClientConnected);
server.OnClientDisconnected += new OnDisconnectedDelegate(server_OnClientDisconnected);
server.OnDataReceived += new OnReceivedDelegate(server_OnDataReceived);
server.OnServerError += new OnErrorDelegate(server_OnServerError);
server.Start();
}
Here is the code for CLIENT :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
using System.IO;
using System.Net;
namespace AdvancedClient
{
public delegate void UpdateText(string txt);
public partial class Form1 : Form
{
Client client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ChangeTextBox(string txt)
{
if (textBox1.InvokeRequired)
{
Invoke(new UpdateText(ChangeTextBox), new object[] { txt });
}
else
{
textBox1.Text += txt + "\r\n";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
{
client = new Client();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.ServerPort = textBox5.Text;
client.OnClientConnected += new OnClientConnectedDelegate(client_OnClientConnected);
client.OnClientConnecting += new OnClientConnectingDelegate(client_OnClientConnecting);
client.OnClientDisconnected += new OnClientDisconnectedDelegate(client_OnClientDisconnected);
client.OnClientError += new OnClientErrorDelegate(clien_OnClientError);
client.OnClientFileSending += new OnClientFileSendingDelegate(client_OnClientFileSending);
client.OnDataReceived += new OnClientReceivedDelegate(client_OnDataReceived);
client.Connect();
}
else
{
MessageBox.Show("E' obbligatorio completare tutti i campi");
}
}
anybody can help me ?
Thanks, Nick.
|
|
|
|
|
It may be blocked by your firewall, or perhaps you do not have a fixed IP address. Going through an ISP on auto-generated IP addresses does not work.
|
|
|
|
|
thanks Richard
so .... which could be the solution?
|
|
|
|
|
I connected my second PC with the phone (hotspot) and I identified public IP replacing it immediately to the local
|
|
|
|
|
That will not work, the 'public' IP only gets as far as the service provider. They then re-route the data to your broadband connection. If you wish to use a socket connection across the internet then you need to get a fixed IP address from your service provider.
|
|
|
|
|
|
I solved the issue.The 4.5 framework does not support destructor.
If someone use the destructor in the class file the solution build properly but the error occur when service is running.So it removed the destuctor and re-build.
Narayan Mitra.
Software Developer.
Narayan Mitra.
Software Developer.
|
|
|
|
|
How to Retrieve Image binary format data store in image
show the image Gridview
|
|
|
|
|
Use a BinaryWriter object.
Small Example:
using (var filestream = File.OpenWrite(file))
{
BinaryWriter bw = new BinaryWriter(filestream);
bw.Write(imagefieldhere, 0, (int)imagefieldhere.Length);
bw.Close();
}
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
|
I am writing an application with Visual Studio 2013 C# that will have a password to enter the program. If the password entered is incorrect, I want to let them know and then close the application. I was trying:
public myapp() {
if (yourguess != password) {
messagebox('sorry');
application.exit();
}
}
But that showed the message and then went on into the program. I want it to show the message and then everything just goes away. Did it in Java, but trying to get it in C#.
|
|
|
|
|
Easiest way?
Look in your program.cs file: it starts your app with the line:
Application.Run(new myMainForm());
One way to do it is to replace that with a form which asks for the password.
Handle teh OK button, and if the user enters it incorrectly, call Form.Close and the app will end.
If he doesn't:
Hide();
myMainForm mf = new MyMainForm();
mf.ShowDialog();
Close(); Will show the main form.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Are you sure that this method doesn't have nasty side effects? Looks a bit ... hacky
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
It's less hacky than opening the password entry form in the form constructor / load event - and if the wrong password is entered the main app doesn't really start at all.
If (as it appears to me from his question) he has the rest of the app in place, it's a less intrusive solution as well.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I don't find an ApplicationContext hard to implement or intrusive.. but if you say this method can't backfire I'll take your word for it
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thx, OriginalGriff, that gave me the idea to be able to get it to work. I added the code for the password to the Main() in program.cs and it worked exactly how I wanted it to. You don't get it right and it lets you know and closes. If you do, it lets you in. Thx!
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I think that is a bad design! The user has to restart the app if he gets the password wrong. I always allow 3 tries before closing the application, caters for the dyslexic idiots we have as users!
Never underestimate the power of human stupidity
RAH
|
|
|
|