|
WidmarkRob wrote: Would that take care of the compiler asking me if I'm missing a cast?
No.
look at the variable type userValue in firstUserNum method and compare it to type yOrn, it's not a string. So the compiler is letting you know there is a problem. If you know your type to be correct, use a cast.
But in this case it isn't, then you need to change the type of yOrn to string, which is the correct type for use with Console.ReadLine().
|
|
|
|
|
Okay, I see you want me to change the variable type.
I should probably create two predefined string variables so there can only be two options.
I'm also thinking that if statement, and then else if… Then moving on to the next number.
Sound right?
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
I change the data type from char to string… That didn't work either
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
D'oh
I forgot to add:
Line
yOrn = Console.ReadLine();
Now it's not complaining.
Thank you
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
Glad you got it working.
You can still get char from string using
yOrn[0]
but you can see the whole string character-by-character using
example^
|
|
|
|
|
Okay, now I'm just starting to build on top of what I've already written.
I get blue squiggly lines underneath:
break;
is it just waiting for me to write some more code so it has something to break to?
I haven't written any more code, yet…
<pre lang="c#">
private static void YesOrNo()
{
string userAns;
Console.WriteLine("If that is correct, press the letter y.\nIf it's not correct, press the letter n.");
userAns = Console.ReadLine();
if (userAns == "y")
{
break;
}
else if(userAns == "n");
{
Console.Clear();
tellUser();
break;
}
//Console.ReadKey();
}
</pre>
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
Nah, that's okay… I'm just practicing creating my own methods. Trying my hardest not to go any further than that for now.
Baby steps
Thanks
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
|
I downloaded it already, thanks… Just haven't taken a look at it yet.
I spent $85 on a book already, a Benjamin on a lifetime membership with learnvisualstudio.net and trying to watch and learn from some YouTube videos.
If you've been following any of my questions/posts you'll notice I'm slowly starting to dig in to more advanced features.
Slowly but surely I'm starting to piece things together.
Sometimes I need to stop and ask for directions.
|
|
|
|
|
WidmarkRob wrote: I spent $85 on a book
Use the best guess
|
|
|
|
|
|
Clearly worth of a 1-vote. Not.
Countered.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Thanks, looking at the current score, I can only assume that the 1 came from some pond life.
Use the best guess
|
|
|
|
|
My 5 to counter the swimmer.
|
|
|
|
|
They don't like it up 'em, Captain Mainwaring!
Use the best guess
|
|
|
|
|
Hi. In theory I think is about right but for some reason it doesn't work. Any ideas? Thank you very much for your help!
public delegate void PullFunction(GameObject o);
if (Object.S1NPCDataController.Name == "name1") return false;
if (Object.S1NPCDataController.Name == "name2") return false;
if (Object.S1NPCDataController.Name == "name3") return false;
[18-1-19] Compile file : 1212.cs
[18-1-19] [COMPILER] Compilation error !
[18-1-19] Line number : 310, Error Number : CS1519, 'Symbol 'if' not valid
[18-1-19] Line number : 310, Error Number : CS1519, 'Symbol '==' not valid
[18-1-19] Line number : 311, Error Number : CS1519,'Symbol '==' not valid
[18-1-19] Line number : 312, Error Number : CS1519, 'Symbol '==' not valid
|
|
|
|
|
Make sure all if conditions are inside a method.
|
|
|
|
|
Hello,
I am new in the land of programming (C# .NET) and I am trying to develop an application that will do HttpWebRequest and HttpWebResponse to an external site.
Basically, I have a username and password for an external site. Once, I login to the external site I'm redirected to a page that has a field for me to enter an ID, and when I submit that ID I get a table with information relating to that ID(for example Name, Address, Telephone, etc.) Well, I would love to develop an application that will allow users of my application to login to the the external site from my App, enter the ID from my App, and get the result table in my App.
Any help on this will be greatly appreciated. Thanks.
|
|
|
|
|
Go to the articles section[^], here on CodeProject. You will find lots of samples to help you.
Use the best guess
|
|
|
|
|
Hi, I want to realise a Cleint/Server chat app that respects this:
1- Server connects to client;
2- server send "Hello" to client;
3- cleint responses with "Hello, ok";
4- server generates RSA Key and send public key to cleint;
5- client select a random algorithm from DES, AES(128, 192, 256) and sends it to the server;
6- server sends "GENERATE FIRST KEY" to client;
7- cleint generate a key with a size that dependse on the algorithm choosen in step 5, and encrypt it using the public key;
8- server decrypts the key;
Now the server and client can communicate using the shared key, and to make it more secure, after 5 min server send "NEXT KEY", to generate another key
Untill now I just can chat between client and server without encrypt messages, and this is my code
Server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Server___Tutorial
{
public partial class Form1 : Form
{
int i;
TcpListener server = new TcpListener(IPAddress.Any, 1980);
NetworkStream stream;
TcpClient client;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
}
public void ServerReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
stream.Read(data, 0, data.Length);
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "Client : " + Encoding.Default.GetString(data);
});
}
}).Start();
}
public void ServerSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnListen_Click(object sender, EventArgs e)
{
server.Start();
MessageBox.Show("Waiting For Connection");
new Thread(() =>
{
client = server.AcceptTcpClient();
MessageBox.Show("Connected To Client");
if (client.Connected)
{
ServerReceive();
}
}).Start();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (client.Connected)
{
ServerSend(txtSend.Text);
}
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client
{
public partial class Form1 : Form
{
int i;
TcpClient client;
NetworkStream stream;
byte[] datalength = new byte[4];
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient("127.0.0.1", 1980);
ClientReceive();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void ClientReceive()
{
stream = client.GetStream();
new Thread(() =>
{
while ((i = stream.Read(datalength, 0, 4)) != 0)
{
byte[] data = new byte[BitConverter.ToInt32(datalength,0)];
stream.Read(data, 0, data.Length);
this.Invoke((MethodInvoker)delegate
{
txtLog.Text += System.Environment.NewLine + "Server : " + Encoding.Default.GetString(data);
});
}
}).Start();
}
public void ClientSend(string msg)
{
stream = client.GetStream();
byte[] data;
data = Encoding.Default.GetBytes(msg);
int length = data.Length;
byte[] datalength = new byte[4];
datalength = BitConverter.GetBytes(length);
stream.Write(datalength, 0, 4);
stream.Write(data, 0, data.Length);
}
private void btnSend_Click(object sender, EventArgs e)
{
if (client.Connected)
{
ClientSend(txtSend.Text);
}
}
}
}
I also have AES, DES and RSA classes, but I don't know how to tell if a message is a key or just a normal message...
Thank you
|
|
|
|
|
The required approach might be defining a protocol and a set of commands in your protocol. Then sending and decoding the message message... Something like
KEY LENGTH KEYDATA (128 bytes or 256 depending on the length)
and decoding the same way in your client.
Note: I wouldn't send symmetric keys in plain sockets. This will defy your purpose of security as someone can easily snoop the key out and set up a man-in-the-middle trap. I would rather consider a SSL socket communication. That includes key exchange and rest intricacies out-of-the box. And I would only focus on my communication protocol.
|
|
|
|
|
Thank you for your answer, yes It s easier if I work with SSL, but this is what my prof wants, so I ve no choice
I didn't undrstand what you mean by (The required approach might be defining a protocol and a set of commands in your protocol)
Can you please give some code to undrstand better,
Thank you
|
|
|
|
|
I'm not that good in programming sockets in c# but in past I've done quite a lot in Java so some out of java base and parts of your code
The protocol:
Contains 4 keywords:
MESSAGE - Generic message <message length="">
KEY - Cipher key <key alg="" length="">
NEXTKEY - Request to generate next key <nextkey>
END - End communication <end>
Each message is terminated by a LF pair (taken from http) and data by CRLF
My idea is read every line, whenever a CRLF is detected, prepare for command parsing.
If MESSAGE command is present, parse the length and display the text that follows.
If NEXTKEY is detected send a KEY message.
If key is detected, read the key and re-initialize the cipher.
public void ServerSendMessage(string msg)
{
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
int length = data.Length;
writer.Write("MESSAGE "+length+"\n");
stream.Write(msg);
stream.Write("\r\n");
}
public void ServerSendKey(byte[] key, string alogrithm)
{
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
int length = key.Length;
writer.Write("KEY "+ALGORITHM+" "+length+"\n");
writer.Write(Convert.ToBase64String(key));
writer.Write("\r\n");
}
At Client
public void ClientReceive()
{
stream = client.GetStream();
StreamReader reader = new StreamReader(stream)
string message = reader.ReadLine();
while(reader.peek() > -1 || !message.Equals("END"))
{
if(message.StartsWith("MESSAGE") {
ParseMsg(message,reader);
} else if(message.StartsWith("KEY") {
ParseKey(message,reader);
}
}
message = reader.ReadLine();
}
private void ParseMsg(string cmd,StreamReader r)
{
int length = Integer.ParseInt(cmd.SubString(8));
char[] msg = new char[length];
r.ReadBlock(msg,0,length);
string message = new String(msg);
}
private void ParseKey(string cmd,StreamReader r)
{
string alg = cmd.SubString(3,3);
int length = Integer.ParseInt(cmd.SubString(9));
byte[] key = Convert.FromBase64String(r.ReadLine());
}
NOTE: Not ready for Production Code... Run tests!
|
|
|
|
|
hi guys,
is there a best practice to save an c# window service's data?
i understand that application data usually use:
-ConfigurationSettings file (appname.exe.config)
-Registry
-Custom XML file
is it the same as for services?
|
|
|
|
|
There is no such thing as a "best practice" specific to a service for saving "data". It depends on the data and the business requirements for handling it.
You also forgot files of other formats and databases in that list.
|
|
|
|
|