|
|
Hey guys. I created a multiple client/server application using sockets in c#. Everything works just fine. Now can you please help me create a code that will allow a client to check if another client is connected to the server(By IP)? I have an idea but I don't know how to implement it. I want something like that:
Client #1 has connected.
Client #2 has connected.
Client #1 sends to server: "check 45.56.128.20"
if client #2 is connected, do nothing. Else:
Server sends to Client #1: "The client is not connected".
My server size code:
http://pastebin.com/vmwJFxFt
My client side code:
http://pastebin.com/YD28bNCg
(I did not write the server&client codes)
Thank you!
|
|
|
|
|
You need to look at the code and work out how it works: we are not here to wade though two complete apps and fix them to do what you wanted.
So far, your total effort on this is a quick google, download, compile, and then try to get someone else to do the hard bits. That's not development: you need to understand code, not just grab it and call it yours. If you don't understand code you download, how do you know that it doesn't do things you really don't want, like introduce ransomware to your computer, or worse your clients?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You are wrong. The codes I showed you are just to show the socket type I use, I improved them and added 200 lines of code but I don't want people to see the source code. I am a software deveploment student and I don't want other people to do the job for me. I just aaked a simple question because I searched all over the internet and couldn't find a solution. If you don't want to help din't help but at least don't say things you don't know.
|
|
|
|
|
I'll just quote your original message:
Quote:
My server size code:
http://pastebin.com/vmwJFxFt
My client side code:
http://pastebin.com/YD28bNCg
(I did not write the server&client codes) So you want us to modify code you don't show us, don't mention in your original post, and then get shirty because we assume you haven't done anything ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am not asking you to modify anything bro, I just asked for help. I have a list named _clientSockets in server and all I am looking for is a way to get the IP Addresses from that list as it is a Socket, and compare it to a specific ip address so I can send a message to the IP. I really don't want you guys to do it for me. I am just tired searching, ive been trying for a week.
|
|
|
|
|
When a client connects, add the complete address (socket & port) to a list. When a client disconnects, remove them from said list. When a client requests whether someone is connected, look in the list and return the answer.
nakash wrote: (I did not write the server&client codes) Doesn't matter; if you want to change the code, than I will expect you to understand each and every instruction in there.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I understand every line but I used another code I took from google that uses the same type of socket in order to give you an indication to help me. Didn't know it is not ok.
But alright, thanks
|
|
|
|
|
nakash wrote: Didn't know it is not ok. It is; but you do agree that you'll have to understand the code you are going to modify
There's also an async client/server socket example somewhere on MSDN. Ehr.. here[^]. They all have some things in common; there's some startup code (modify to create a list), there's some connection code (modify to add socket-address to list).
If you're unsure if a connection is still alive, then send some data to it; it will raise an exception if it can't be sent or received.
Alternatively, you could search CodeProject for articles like "socket chat" applications; should be a few there
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thank you very much! I really appreciate it
Going to learn more now.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
i downloaded the zip file and unzip it but when i open .sln file given in the folder its showing that unable to open file bcz its created in higher version ,and i m using visual stdio 2010.plz resolve my issue
|
|
|
|
|
|
Ummm... the answer is rather obvious. Upgrade your version of Visual Studio.
|
|
|
|
|
|
Depending on the user input I create a certain number of textboxes using a loop. For example if user inputs "10" then 10 textboxes are created. TxTBox1, TxTBox2 etc etc.
How then do I create identical event handlers for these textboxes for example to validate input for them. They will all be exactly the same.
|
|
|
|
|
Try:
void AllTheseTextBoxes_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
string input = tb.Text;
...
}
}
private void MyButton_Click(object sender, EventArgs ew)
{
for (int i = 0; i < 10; i++)
{
TextBox tb = new TextBox();
tb.Text = "Textbox " + i;
tb.TextChanged += AllTheseTextBoxes_TextChanged;
...
}
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff has shown you how to make all run-time created Controls use the same EventHandler. Let me add:
1. when you create a set of Controls at run-time: you probably should keep track of them ... keep a reference to the instances for future use: I got tired of writing Control specific code for this, and created a generic method:
public Dictionary<string, T> CreateControls<T>(Control container, int ncontrols, Func<T, int, string> nameGenerator, Func<T, int, Point> locGenerator)
where T : Control
{
Dictionary<string, T> dict = new Dictionary<string, T>();
string name;
for (int i = 0; i < ncontrols; i++)
{
T newT = Activator.CreateInstance<T>();
container.Controls.Add(newT);
name = nameGenerator(newT, i);
newT.Name = name;
dict.Add(name, newT);
if (locGenerator != null)
{
newT.Location = locGenerator(newT, i);
}
}
return dict;
} Sample usage:
int hstart, vstart, hoff, voff;
hstart = 100;
vstart = 100;
hoff = 0;
voff = 60;
var tbxDict = CreateControls<TextBox>(this, 5, (tbx, i) => $"newTbx_{i}", (tbx, i) => new Point(hstart + (i * hoff), vstart + (i * voff)));
var check = tbxDict["newTbx_0"].Name; When this is executed, five new TextBoxes are added to the Form this code runs in, a Dictionary is created and returned for easy future access to the created TextBoxes, and the new TextBoxes are placed on the Form in a column starting at 100,100. offset from each other vertically by 60 points.
The two functions (Delegates) that create the name and position of each new TextBox are passed as parameters to the 'CreateControls method.
Now, validation: that's the next chapter
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
VB6 is long dead, but it had one function I really found useful.
It was called ProperCase() , or something similar.
What it did was apply upper casing to the first letter of each word and make the rest of the word lower case. It was great for names.
Does C# have anything that performs that function?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
|
Thanks Eddy!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
HI,
I am trying to figure out how to convert my nested object in to not nested JSON...
I am writing this in .net MVC
This is my object (its a list)
emp { Country: "USA",
Grade: "2B",
Data: {
Name:abc,
Value:155
},
{
Name:abcd,
Value:200
},
{
Name:abcd,
Value:300
}
},
I would like to be able to convert it to JSON, while Changing the "Data" as new object where name is a name of object and value is its value....so something like this
emp { Country: "USA",
Grade: "2B",
abc: 155,
abcd: 200,
abcde: 300
},
I am still learning, and not sure how to go about it, didn't work much with json before. Would appreciate information if its possible and some tips how to solve it
|
|
|
|
|
You haven't shown the code you're using to generate the JSON.
At a guess, you'll need to make your emp property a Dictionary<string, object> :
emp = new Dictionary<string, object>
{
["Country"] = "USA",
["Grade"] = "2B",
["abc"] = 155,
["abcd"] = 200,
["abcde"] = 300,
},
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|