|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On July 08 2013 00:11 mustache wrote:C++ TCP socket programming question: I've written a small chat program where the client can send a message to the server. i'm using the following piece of code to establish the connection
sockaddr_in connectionInfo; connectionInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); connectionInfo.sin_family = AF_INET; connectionInfo.sin_port = htons(54345); int connectionInfoLen = sizeof(connectionInfo);
connect(clientSocket, (struct sockaddr*)&connectionInfo,connectionInfoLen);
this works perfectly fine when im using my own pc as both client and server, the IP i enter on both sides is simply 127.0.0.1 But what IP do I use if I want to connect to my friends PC if we both have a router in between? only the router's IPs are unique, so wouldnt i have to specify both the unique router IP as well as the internal IP of the PC? I searched google but I couldnt find anything related to this, possibly because im not searching for the right keywords. Your friend has to change his router settings so that connections to the port (54345 I presume) get redirected to his actual PC. Then you only have to know his router address
|
On July 08 2013 00:19 MaGariShun wrote:Show nested quote +On July 08 2013 00:11 mustache wrote:C++ TCP socket programming question: I've written a small chat program where the client can send a message to the server. i'm using the following piece of code to establish the connection
sockaddr_in connectionInfo; connectionInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); connectionInfo.sin_family = AF_INET; connectionInfo.sin_port = htons(54345); int connectionInfoLen = sizeof(connectionInfo);
connect(clientSocket, (struct sockaddr*)&connectionInfo,connectionInfoLen);
this works perfectly fine when im using my own pc as both client and server, the IP i enter on both sides is simply 127.0.0.1 But what IP do I use if I want to connect to my friends PC if we both have a router in between? only the router's IPs are unique, so wouldnt i have to specify both the unique router IP as well as the internal IP of the PC? I searched google but I couldnt find anything related to this, possibly because im not searching for the right keywords. Your friend has to change his router settings so that connections to the port (54345 I presume) get redirected to his actual PC. Then you only have to know his router address
but which pc port would the router connect to? And how would this work if he has a router connected to a router connected to a wireless router? or can all this be set in the router settings?
also how would this normally work? its not like you have to reconfigure your router based on what program you're running...
|
On July 08 2013 00:25 mustache wrote:Show nested quote +On July 08 2013 00:19 MaGariShun wrote:On July 08 2013 00:11 mustache wrote:C++ TCP socket programming question: I've written a small chat program where the client can send a message to the server. i'm using the following piece of code to establish the connection
sockaddr_in connectionInfo; connectionInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); connectionInfo.sin_family = AF_INET; connectionInfo.sin_port = htons(54345); int connectionInfoLen = sizeof(connectionInfo);
connect(clientSocket, (struct sockaddr*)&connectionInfo,connectionInfoLen);
this works perfectly fine when im using my own pc as both client and server, the IP i enter on both sides is simply 127.0.0.1 But what IP do I use if I want to connect to my friends PC if we both have a router in between? only the router's IPs are unique, so wouldnt i have to specify both the unique router IP as well as the internal IP of the PC? I searched google but I couldnt find anything related to this, possibly because im not searching for the right keywords. Your friend has to change his router settings so that connections to the port (54345 I presume) get redirected to his actual PC. Then you only have to know his router address but which pc port would the router connect to? And how would this work if he has a router connected to a router connected to a wireless router? or can all this be set in the router settings? also how would this normally work? its not like you have to reconfigure your router based on what program you're running... Basically, to get around NAT (Network Address Translation) you always have to configure to router to do it. There are possibilities to do that remotely from your side by talking to the router (keyword: NAT traversal), but I have no experience with that so I can't tell you exactly how.
Multiple routers should not be a problem. If he is using the routers as a LAN switch only, you should have to do nothing special, since after the first router it's all a single LAN with unique IPs. If they are cascaded I suppose that you have to configure each one of them, but don't take my word for it.
The port which your friends router uses to connect to his PC (that's what I gathered to be your question) does not really matter, since from the outside you are only talking to the router and the router then does the mapping of IPs and ports.
|
On July 08 2013 00:11 mustache wrote: C++ TCP socket programming question:
I've written a small chat program where the client can send a message to the server.
So you have a server running? I'd let the clients connect to the server, let the server keep a list of currentlyConnectedClients and everytime a message arrives I would broadcast the message to each connectedClient as long as it's not the sender.
So basically all clients only need to know the server IP.
If it's a (small) local environment I would just send a connect-message to every possible IP and if you get an answer the server is found. Should be np for LAN etc, since your router would just act as a switch in your local network.
If you want something like PC-Router-[Internet]-Router-PC you would need to configure NAT on the routers as mentioned before. And your friend would have to tell you his IP.
|
Thanks for all the answers so far. I will try configuring the router and see if it works like that.
One more question. with a program like MSN messenger, how do they get around the problem of router configuration? NAT traversal as mentioned? or do routers have standard settings for ports they forward to certain IP
|
MSN is connected to a server, and your client knows its address (or a way to get it). With outgoing connections, the NAT just does its job and lets you connect to the server. Then messages can go through the server and that allows group messaging too.
If you have a server, you can also try to get a direct connection between two clients between a NAT (that was used by Skype, don't know if it's still used today). Basically, you lure both NATs to believe the connection is an outgoing one. But for that you need a third party (the MSN server here) to coordinate the connection.
|
|
|
Java>>Threads Question:
Running some arbitrary simple code (printing first n even numbers or whatever) on 1 or more threads results in roughly the same execution time.
Is this expected? Seems really surprising to me.
|
Yes. There is overhead for using multiple threads (such as synchronisation) and simple code rarely benefits from multithreading.
|
On July 09 2013 03:53 n0ise wrote: Java>>Threads Question:
Running some arbitrary simple code (printing first n even numbers or whatever) on 1 or more threads results in roughly the same execution time.
Is this expected? Seems really surprising to me. It very much depends on the code and environment. If all of the threads are competing for a single resource (e.g. a file descriptor), its expected that execution would see no speedup. If all the threads are running on a single core (and thus not actually parallel), its expected that execution would see no speedup.
|
On July 09 2013 11:09 tec27 wrote:Show nested quote +On July 09 2013 03:53 n0ise wrote: Java>>Threads Question:
Running some arbitrary simple code (printing first n even numbers or whatever) on 1 or more threads results in roughly the same execution time.
Is this expected? Seems really surprising to me. It very much depends on the code and environment. If all of the threads are competing for a single resource (e.g. a file descriptor), its expected that execution would see no speedup. If all the threads are running on a single core (and thus not actually parallel), its expected that execution would see no speedup.
they're not competing for a single resource, but how do you setup multi-core execution?
le - if you're referring to ExecutorService, I've also tested using that, obtaining the same results.
|
You mentioned printing, which could well be the single resource. If you have a bunch of real code in there, it's probably not a bottleneck, but if your code is literally just "print this shit"...
Also
for (i = 0; i < ... ; i+=2) { printf (i); }
does not parallelize, especially if you need stuff to be printed out in order.
|
On July 09 2013 14:40 phar wrote: You mentioned printing, which could well be the single resource. If you have a bunch of real code in there, it's probably not a bottleneck, but if your code is literally just "print this shit"...
Also
for (i = 0; i < ... ; i+=2) { printf (i); }
does not parallelize, especially if you need stuff to be printed out in order.
they're not printing in order, but they are indeed printing... interesting. I'm basically trying to test the benefits of using more threads, without much success so far 
from what i've read, it may also be because jvm automatically optimizes the workload on all cores by default.
le2: right, ok, tested without printing (just incrementing a personal field) and the execution times make much more sense now
much obliged, gentlemen
|
On July 09 2013 15:53 n0ise wrote:Show nested quote +On July 09 2013 14:40 phar wrote: You mentioned printing, which could well be the single resource. If you have a bunch of real code in there, it's probably not a bottleneck, but if your code is literally just "print this shit"...
Also
for (i = 0; i < ... ; i+=2) { printf (i); }
does not parallelize, especially if you need stuff to be printed out in order.
they're not printing in order, but they are indeed printing... interesting. I'm basically trying to test the benefits of using more threads, without much success so far  from what i've read, it may also be because jvm automatically optimizes the workload on all cores by default.
Using multiple threads on a multi-core processor will only yield results if you have a parallelizable algorithm. And the speedup will be limited by the non-parallelizable portion of your algorithm.
In your case, it takes way more CPU cycles to print to the output than to compute even numbers, so you do not gain anything by parallelizing it.
|
On July 09 2013 15:53 n0ise wrote:Show nested quote +On July 09 2013 14:40 phar wrote: You mentioned printing, which could well be the single resource. If you have a bunch of real code in there, it's probably not a bottleneck, but if your code is literally just "print this shit"...
Also
for (i = 0; i < ... ; i+=2) { printf (i); }
does not parallelize, especially if you need stuff to be printed out in order.
they're not printing in order, but they are indeed printing... interesting. I'm basically trying to test the benefits of using more threads, without much success so far  from what i've read, it may also be because jvm automatically optimizes the workload on all cores by default. le2: right, ok, tested without printing (just incrementing a personal field) and the execution times make much more sense now much obliged, gentlemen They may have not been printing in order due to unhandled race conditions. You have no control over what threads get the processor(s) at any given time, and so unless you explicitly use some concurrency library (Locks are a good one in Java), you can't trust anything involving timing, which includes ordering.
If you're only concerned with time-to-complete, and not the correctness of the output, don't print until the very end. Printing is painfully slow compared to practically everything else, and it is likely ruining any benefit of parallelization.
|
this is conjecture, so take this with a grain of salt. If someone knows that something I've said is wrong, feel free to correct me-I'm basing this on my understanding of how parallelism works (my experience is mostly with MapReduce in Java and OpenMP in C)
there's no benefit if you're doing only one thing because there is nothing to parallelize. like phar said, it's likely that because in the first case all you were doing was printing, since every thread wants to dump something to System.out at the same time only one thread can execute at a time.
In fact it's probably slower than the serial case because of the overhead in dividing the work.
|
I've been wondering why software uses Windows Registry. As far as I know, the same can be done with a simple .conf file, and it's even portable, while Windows Registry needs to be manually exported to the new computer. Maybe there's something I'm missing as a detail?
Edit: Maybe users are that dumb that developers decide to hide config within the registry so that config files aren't deleted? Or is there another advantage?
On the other hand, http://en.wikipedia.org/wiki/Windows_Registry#Rationale sounds good to have individual registry for each user, but the same can be done with just config files. E.g. store configs in C:\Users\user or some other folder.
|
On July 10 2013 21:04 darkness wrote: I've been wondering why software uses Windows Registry. As far as I know, the same can be done with a simple .conf file, and it's even portable, while Windows Registry needs to be manually exported to the new computer. Maybe there's something I'm missing as a detail?
Edit: Maybe users are that dumb that developers decide to hide config within the registry so that config files aren't deleted? Or is there another advantage? I guess the only real advantage is that it's central to the whole computer, and I think the initial idea was to only store computer-wide settings there, concerning windows. Then it was opened and every single program decided that it would be a good idea to save settings in the registry.
It makes no sense to me either, anyone who has used linux for a decent amount of time will agree that it's far nicer to edit .ini and .cfg files than the fricking registry.
|
On July 10 2013 21:04 darkness wrote:I've been wondering why software uses Windows Registry. As far as I know, the same can be done with a simple .conf file, and it's even portable, while Windows Registry needs to be manually exported to the new computer. Maybe there's something I'm missing as a detail? Edit: Maybe users are that dumb that developers decide to hide config within the registry so that config files aren't deleted? Or is there another advantage? On the other hand, http://en.wikipedia.org/wiki/Windows_Registry#Rationale sounds good to have individual registry for each user, but the same can be done with just config files. E.g. store configs in C:\Users\user or some other folder.
I don't use it ever, but I can imagine it's useful for stuff you don't mind being (or that you want to be) globally accessible. The user can (well, should be able to) pick the place your program is installed to. They're not going to care about picking where in the registry your program writes data.
Take SC2; it writes APM data to the registry so that some razer devices can use it (the headphones that change colour). The registry lets them do that in a standard, pre-agreed place where it's not going to get in the way. That seems like a reasonable use of the registry to me.
|
On July 09 2013 15:53 n0ise wrote:Show nested quote +On July 09 2013 14:40 phar wrote: You mentioned printing, which could well be the single resource. If you have a bunch of real code in there, it's probably not a bottleneck, but if your code is literally just "print this shit"...
Also
for (i = 0; i < ... ; i+=2) { printf (i); }
does not parallelize, especially if you need stuff to be printed out in order.
they're not printing in order, but they are indeed printing... interesting. I'm basically trying to test the benefits of using more threads, without much success so far  from what i've read, it may also be because jvm automatically optimizes the workload on all cores by default. le2: right, ok, tested without printing (just incrementing a personal field) and the execution times make much more sense now much obliged, gentlemen
If you really want to see speedup from Multi threading (and to some extent, cache hits) I would suggest you try to implement Matrix Multiplication or Computing Primes. Some modifications to said algorithms allow for a bigger speedup to it's single thread counterpart, and you can also check the difference in cache access vs ram access by splitting the work in blocks (for both single and multi thread, the difference is really HUGE)
|
|
|
|
|
|