I'm currently working on trying to create a very basic bare bones instant messenger with Java.
I'm at a bit of a loss where to begin, was wondering if anyone here could reccommend a starting point? What I should read? What would be the easiest way for a beginner/intermediate programmer to try and achieve this?
Currently, I'll I'd want it to be able to do is to send text to one machine to another. I am so far unfamiliar with anything network related in java. Would I use a server or just direct connect between two machines?
Any pointers/direction or advice anyone has in this matter would be very helpful!
Firstly check out the official resource for Socket programming, the article on writing a Client/Server Pair should provide you with what you need to know to create a instant messenger.
Well, I am focusing the second question of yours.
If you're building this program for your local area network and if you're intended to use socket programming, you need to find the exact ip address of the machine. If you don't know the ip address ( I mean if you don't want the program to know ip address before you run it ) ,then you can ping local ip addresses until you find some peers.
Some of the p2p applications use servers as the ip matchers. This means that the servers have ip address tables which make all of the users send/receive data by using these servers. But if you just started on networking with this project, I think don't use server.
Lastly, you can take a look at a project of my own. It's a simple parallel document preparation program with a chat module inside it. It's developed in C#. It needs wireless ad hoc network to run. Link : http://code.google.com/p/parallel-docs/
I don't think this type of project is ever going to be a beginner project. I'm absolutely not discouraging you to try it, I think you should. I'm always in over my head with stuff like this but I look it up and ask questions and usually end up learning something. My advice would be no matter how simple of a program it is, start with some Class Diagrams, if nothing else just to get organized before jumping into the code. For example you know you you'll need a user account object right off the bat and you know you'll need certain attributes for that object (username, password, etc.), I encapsulate everything so then you would need methods to get and set these fields and so on. I've found that by doing that even if I don't create complete proper UML diagrams getting the easy stuff out of the way and if somewhat organized first if nothing else frees me to focus on the more complex logic and things I may not yet know how to implement.
Related
sorry if my english isn't perfect.
I'm trying to make an app and I need to exchange information between more devices.
I thought that could be a solution connect the devices on a server but I really don't have the idea where start.
What language I need to study to make this? There is a better solution?
This highly depends on what you are trying to achieve in the first place. It would be helpful if you could tell what you are trying to do, but I will still outline some general aspects:
You need to decide, what information is going to be exchanged and how this should happen
What information: Figure out, what exactly needs to be sent and received. Generic text messages? Images? Byte Streams?
How should this be done: Generally spoken, there are two approaches of getting information as a client: Polling and subscribing.
Polling: This approach means to periodically check an endpoint for new data. For example, HTTP uses this way: A web browser or any other client (REST-Client for example) periodically requests information from a HTTP-Server, using a connection just for this single request.
Subscribing / Sync / Notification: In some way or another, the client tells the server that it is interested in the information and wants to get notified when there is something new. The connection is initiated at the beginning and held open for further usage. The benefit of this approach is that changes are received immediately, but on the other hand a permanent connection needs to be maintained.
Things to study
At the beginning, get a good understanding of the TCP/IP Protocol, how Sockets work, how common Protocols do their job (e.g. HTTP, WebSockets)
Take a look at specific Protocols working on top of the basic ones
Tip: REST: Most common WebServices Protocol, providing a common way to exchange stateless data. Uses Polling.
WebSockets: Socket connection using Web Browsers. Commonly used to update information without needing to poll.
There is no specific language to learn for connections. It's more about understanding what the difficulties are and what ways have been invented to address this. Once you get to this point and know what you want to do, it's possible in every language.
Recommendation: As you seem to use Java/Android, I would try to use REST. A really great client-side library for REST on Android is Retrofit. For the server side use what fits for you .. common Java way would be to use Jersey, but you are free to choose from a lot of choices. If using Jersey is too hard for the beginning, maybe take a look at the JS/NodeJS world, those guys invented Express, which allows you to create a REST service out of just a database, wihtout having to code a lot.
First you need to decide if you want to go for an Android or an iOS application. There are other various mobile operating systems as well, but these are widely used . If you want to go for android which is most widely used in my opinion, then you need to learn Java. If you want to go for iOS application, then you need to learn swift or objectiveC. These languages provide the API to connect with various types of services such as Facebook, Firebase and Amazon etc. If you want to connect to some other local server who’s IP is known to you, then you can use socket programming to send messages.
There could be many ways you can implement this. One way will be using Web services. Of course REST might be a better option, if you follow this approach. You can implement Your service(server side code) with any language. I will recommend you use java since you are already using android.
Aside from this You might need to go through the basics of REST, its specifications and
some reference implementations for language of your preference.
I want to make a chess online game in Java. I am able to write the game offline, that is no problem, but now I want to make it online so people can play over the internet.
Unfortunately i have no actual idea of how to do this so here are my questions:
What kind of server do i need? (like webserver, xyz...?)
How can i save the Chess board on a server?
Do i have to make this with sockets and make it with the MVC? (so the client is a dumb buddy which only asks if this move is valid, if so it sends the info to the server which says yes/no and then makes the move and sends the new board to the clients again?
Will i need Threads to make multiple instances of the boards on the server so multiple people can play at once?
Thanks to everyone showing the effort trying to help me! As you can see i am new to this and i do not know where to start, but i really want to make this!
You could set up a Tomcat server somewhere and put your programmed servlet there, but for you that may be a bit to much (either renting one or configurating one on a local machine). As an alternative one of your local PCs can just listen on a port, which is usually managed by the tool you use to communicate.
The basics for loading and saving to disk are done with FileInputStream or FileOutputStream. It doesn't matter if your JVM is on your local PC or on a remote system. The advanced tricks are Serialization and APIs like Simple XML for Java.
I'm not sure if I understand the question. If you're asking where the validation of the move should take place, that is personal taste, although I think it should take place on client and server. If you're asking how client and server should communicate, I think Remote Method Invocation (RMI) would be a good start for you. I came to love Akka, but I think that one is a little bit too much, but you can look into it, if you like. Sockets are to RMI and Akka what is going by foot to driving a bike or a car. It's good to understand communication, but not helpful for direct use in higher level programming (as in games).
Each game could run in own thread, yes.
I think a good exercise for you would be to look up all words unknown to you from my answer, there is much to learn.
I am coding a program that necessitates having two machines talk to each other via TCP.
I have a (very) basic familiarity with the java.net Socket classes and related things. I am worried about routers/firewalls blocking the connection. All I need to do is get a port open in order to use the Java STL Socket classes.
I was told to look into using upnp by a friend, and another stackoverflow question regarding upnp suggested using Cling. As a network programming novice, Cling seems like overkill for this (and I am not advanced enough to fully understand the manual).
I am sorry if I am asking the wrong question, looking in the wrong place, etc. All I want to do is connect two machines to send some floats back and forth without having to manually go into my router and forward ports.
Is there anything in the STL that does this automatically, or any simple libs?
First thing first: there's no such thing as "STL" in Java. You're probably referring to the basic Java SDK.
Second, you are asking for "simple means" to perform an operation that is, from a networking point of view, not trivial at all. Look at the most basic task that one of your programs (program A) will have to do: denoting the network location of its "target" (program B). If program B is behind a router, what do you know about program B's network location? nothing. All you know is the router's address.
Same with firewalls. You're concerned about firewalls blocking your connection? well, if your firewalls are any good, then you want them to block all connections except for pre-approved ones.
For programs separated by a NAT, the only sensible method to go about doing what you're looking for is to use UPnP. If Cling is an overkill, try something like weupnp.
There's no free lunch, though.
If there is no network "masking" device (such as a router) between program A and program B, then very simple TCP/IP programming using the Java SDK should just work. If there are networking devices along the ways, you'll have to cope with them by means of configuring your devices, or using UPnP.
I would take a long look at the following tutorial: http://www.giantflyingsaucer.com/blog/?p=224
The idea here is that a library called xsocket creates events that trigger when new information arrives.
I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.
I have a simple basic program in mind to keep my kids off certain websites for prolonged periods of time.
Basically, I would like to run a little background process that would have something like this :
if (user is on website for > 20 minutes every 24 hours )
{
browser redirect user to specified website
}
I realise I could easily get a third party program that would do this, but I'd enjoy doing it myself ! The problem is that I have absolutely no idea how to understake such a project - or even what languages I could use to make something like this. I have a fairly decent knowledge of PHP and Java and am always learning, so any suggestions for a way to go about this would be really appreciated !
Thanks a lot :)
There is no simple basic program that will do this.
Something like this requires that they are always using the same browser, and that it publishes an inter-process API that allows you to see what websites are being viewed and navigate to other websites. That's a lot of assumptions. Your kids could always use a different browser, or they could kill the process on the system. No matter what software you install on their machine, there is almost invariably a way for them to get around it. There's not going to be an easy way to do it.
The best solution is hard. Basically, it's firewalling. Set up a firewall in your house and put a filter on packets being forwarded. Inspect the packets to find HTTP requests, and intercept them. Log what's being requested and if it exceeds your limit, rewrite the packets how you want.
It might be a better idea to just look for a commercial software that does what you want.
If you are using FireFox, you can probably achieve this with a GreaseMonkey script.
I think you could achieve that kind of feature with a programmable proxy. In the ruby world, there was, during _why's existence, the fantastic MouseHole. Unfortunatly, those times are gone. Nowadays, using Java, I would take a look at things like this document. Finally, you can also take a look at proxomitron.
I see two simple way to approach this problem:
You can write an extension for your preferred browser. Look, for example, at Firefox Building an Extension tutorial.
You can write a http(s) proxy and set your browser to use it.
I do not recommend the 2nd one, IMHO it requires too much work for a simple in-home solution.
Short answer is that this is a fairly difficult problem to solve because of the reasons cited by Erick.
The only way to do it would be to set up something along the lines of WireShark that would analyze all network traffic on the machine and look for requests made to a certain url pattern (facebook) and start a timer whenever it happens. However, there are additional complexities when one wants to say, "I don't want to spend more than 20 minutes here a day" because it's nearly impossible in todays web to define what it means to spend 20 minutes at a site. See this thread on the OS X Hints forums for a helpful discussion on why this is hard. It basically boils down to the way websites do requests for you these days rather than you manually clicking. Of course, if you don't care about the user actually being there, but just giving them 20 minutes a day from the first time the log on to the site, then the problem becomes a little easier.
You could figure out how to write software for your router as one possible project. Using Tomato or DD-WRT gets you the ability to control what's running on your router and would give the centralized access you need to place a sniffer on the whole network (especially if you have multiple PCs you're trying to do this to). Of course, that adds the complexity in of trying to figure out what it means for one person to spend time on the website over against another. If all your PCs are single user then the problems not that hard but if you have multiple users per PC then you run into not even being able to key off of the IP address as a unique user.
If you don't want to program your router, then you'd have to write up a network sniffer and install it on every machine. Something like jNetStream might give you a nice head start as writing a network sniffer probably doesn't qualify as a "simple" program like you thought you were going to write.
Anyway, once you get that set up and running you'd just have to figure out how to hook it into your OS. I doubt any language is really "inappropriate" for that task so have at it!
You need to program a (smart) http proxy server for that. This is generaly considered a non trivial activity. But hey, there are hackers out their who say it can be done:
A HTTP Proxy Server in 20 Lines of node.js Code
(It uses the node.js library)
Just add you're own logic and there you are. King of the proxy hill ;-)