I'm kinda interested in with java sockets in these days and i want to read my mails from gmail with using java socket. is it possible?
Socket s;
s = new Socket("imap.gmail.com", 993);
InputStream in;
in = s.getInputStream();
BufferedReader sin = new BufferedReader(new InputStreamReader(in));
PrintWriter output = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
output.println("a001 LOGIN my-e-mail my-pass");
output.flush();
while ((line = sin.readLine()) != null)
System.out.println(line);
s.close();
what should i do after connecting to socket?
thanks in advance. (notice: i dont want to use java mail api, things get really easy with it, i'm just choosing this way to get familiar what's going on behind the scene)
edited the code.
If you feel like getting familiar with IMAP at GMail, you're going to have to put SSL/TLS on top of it. All GMail traffic is encrypted.
Once you have a socket you implement the IMAP protocol on it.
https://www.rfc-editor.org/rfc/rfc3501
+1 for the question.
EDIT: Yes, it is possible to speak to IMAP server using java sockets and in some cases it might be advisable to do so rather than using java mail library.
Using javamail is the easy way out. It is also appears to be the obvious solution but, there are cases when you do not want to go that route.
Javamail is highly unsuiatble for scaled out deployments with a large number of concurrent users that need to be kept notified on near real time basis.
Say, you want to service more than 1000 users per jvm and want to keep the overhead of creating imap connections low then, you will resort to implementing sockets yourself and write IMAP commands in the socket itself. Needless to say, other benefits include near instantaneous delivery of a new email to your user i.e if you are using AJAX.
Near instantaneous email is one of the few things left to like about Blackberry. Given RIMs scale, I don't think they would be using Javamail on top of IMAP to push out emails. That would be highly non-performant.
Related
I have a Java server that will have two difference types of clients, a Java based console, and a Lua based client that the server will be controlling. I have got the Lua client to talk back and forth with
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
I am now trying to get a Java based client to connect and I would like to use
out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
The problem I am having is understanding the best way to go about accepting connections, not knowing if it is a Lua or Java client on the other end. Then setting up the proper streams.
Should I just use different ports on the initial connection? I was hoping something more along the lines of sending a string from ObjectOutputStream that BufferedReader could read. Or should I use something lower level to see what is connecting before setting up if it is PrintWriter or ObjectOutputStream.
A link to an in depth tutorial or actual code would work. I am having trouble finding resources beyond the basic setup of one or the other.
Thanks in advanced.
Inasmuch as you propose to provide what sounds like two completely different services, it would be natural and appropriate to provide them at different ports. To implement that, your server would create and manage two separate ServerSockets, listening on different ports. One would provide one service; the other would provide the other. You could use either threads or a Selector or a combination of those to handle the two server sockets and all the clients.
As a separate matter, I urge you to think carefully before relying on Object streams. If you can implement your service on top of a simpler and/or more general protocol then you will avoid the multiple joys of Java Serialization, and you will also leave open the door for clients written in other languages.
I want to create chat server and I need to be able to do some things like clear the screen of a specified client, I thought about it and I figured that it would be a good idea if I would send a keyword to the client:
clientSide eg:
if((sporocilo = bufferedReader.readLine()) != null){
if(sporocilo.equals("clearTheScreen"{
object.clearMyScreen
}
}
However there are a lot of commands and the source code would be huge, plus once a client figures out the keyword he could exploit it, how could I do that in another, better way?
Take any example like FTP. Port 20 is used for default data and port 21 is used for Control(authentication etc).And different port mean different socket. After all socket is IP + Port no.
Same is in your case. Use different ports for actual chat data and your service instructions(like clear screen though i don't see why server must clear clients screen). Also instead of string commands like "clearTheScreen" use service ID's and keep a mapping of service ID to functions to be executed. Because if you keep simple string with names like "clear" or "password" with it then it will be easy for other to manipulate data(if it is not encrypyted ofcourse which is another good way to send data).
I've been messing a lot with TCP/IP Communication the last few days (Using Java and C#). I understand how it works and am able to use it. My Question is more a code design question, how its done the best and easy way to make a real communication.
For Example ive Built my own Multiuser Chat Server. I want my Communication to be able to decide wather its an Auth request, or a new chat message the ability to get the current user list etc etc.
Ive implemented a few ways on my own, but im not quite happy About that since i think theres a more standard and beauty way to do this.
My first thought was a String with Delimiters wich gets splitted, here is the Example of my Implementation of my Communication in Java:
//The Object-types im Using
clientSocket = new Socket(host, port_number);
_toServer = new PrintStream(clientSocket.getOutputStream());
_fromServer = new DataInputStream(clientSocket.getInputStream());
//Example Commands my Client sends to the server
_toServer.println("STATUS|"); //Gets the Status if server is online or closed (closed can occur when server runs but chat is disabled)
_toServer.println("AUTH|user|pw"); //Sends an auth Request to Server with username and Password
_toServer.println("MESSAGE|Hello World|ALL"); //Sends hello World in the Normal Chat to all Users
_toServer.println("MESSAGE|Hello World|PRIVATE|foo"); //Sends hello World only to the user "foo"
_toServer.println("USERS|GET"); //Request a list of all Connected Users
//Example In the Recieved Message Method where all The Server Messages Get Analyzed
serverMessage = _fromServer.readLine(); //Reads the Server Messages
String action = serverMessage.split("|")[0];
if (action.equals("USERS")) { //Example "USERS|2|foo;bar"
String users[] = serverMessage.split("|")[2].split(";");
}
if (action.equals("MESSAGE")) { //Example "MESSAGE|Hello World|PRIVATE|foo"
if(serverMessage.split("|")[2].equals("ALL") {
//Code and else for private....
}
}
if (serverMessage.equals("STATUS|ONLINE")) {
// Code
// I leave out //Code and } for the next If statements
}
if (serverMessage.equals("STATUS|OFFLINE")) {
if (serverMessage.equals("AUTH|ACCEPTED")) {
if (serverMessage.equals("AUTH|REJECT")) {
Is this the way its normally Done? Ad You See I need to send Statuscodes and Objects Corresponding to the Code. Ive Thought about Writing the Data in Bytes aswell and Implementing a "Decoder for Each Object", Example:
int action = _fromServer.readInt();
//opcodes is just an Enum Holding the corresponding int
switch(action) {
case(opcodes.MESSAGE):
break;
case(opcodes.AUTH):
break;
}
Note that this is more over a general design Question not just for this Chat Server Example, I think im Implementing a little Network Based Console Game just for Practise.
Is there a better way to do this or even an API/Framework?
Thanks in advance!
Essentially you're designing a protocol. There are a number of communication protocols that can handle this, the main one that comes to mind is IRC. I'm sure you can do a web search for tips on how to implement the protocol.
As for extending something like this for a console game, well I would start with implementing IRC, and using that to learn how real communication protocols are written. Once you've done that you can build on it to add your own commands to your framework.
If you are designing a protocol for inter-language communication, I would suggest not to use formated Strings as a means of communication but statusbytes. If you consider for example the design of TCP/IP itself you will find, messages consist of a fixed-format header and a variable payload. That way you always know, that (e.g.) the third byte of the message contains the messagetype, the fifth denotes an errorstate and so on. This makes handling easier.
If you have designed your protocol, you could consider working with explicit MessageObjects on the java-side, in which case you would implement a factory with marshalling and unmarshalling methods for these objects, converting objects from and to messages in your protocol.
If you are all-java you can even spare that effort and use ObjectInputStreams and ObjectOutputStreams on client and Server. If you are not, you might want to take a look at the Google Protocol Buffers: http://code.google.com/intl/de-DE/apis/protocolbuffers/, which do essentially the same for inter-language communication.
If your project grows, you may want to have a look at Netty - it's a framework for dealing with communication code. If your code is simple, you will be better off doing things manually.
As for protocol design, it depends on what is most important for you: performance, extensibility, human-readability, ease of debugging etc. These criteria may oppose each other to some degree, for example high performance may mean preference for binary protocols, but these negatively impact ease of debugging and sometimes extensibility. It's usually a good idea to not reinvent the wheel. Get inspired by existing protocols. If you choose to go binary, don't start from scratch unless you really have to, start with Protocol Buffers. If your app is simple and not aimed at very high performance, use a human-readable protocol which will make your life easier (debugging and testing are possible with standard shell tools such as strace and nc).
I think Apache MINA will help you. http://mina.apache.org/
Building a Java C/S application is really complex, you need to deal TCP, UDP and multi threads programming; MINA can help you for these things.
I think the other part you need is your private chatting protocol, but how about the open sourced IM service like Jabber? :)
i wrote a bit of code that reads download links from a text file and downloads the videos using the copyURLToFile methode from apaches commons-io library and the download is really slow when im in my wlan.
when i put in an internet stick is is about 6 times faster although the stick got 4mbit and my wlan is 8 mbit.
i also tried to do it without the commons-io library but the problem is the same.
normally im downloading 600-700 kb/s in my wlan but with java it only downloads with about 50 kb/s. With the internet stick its about 300 kb/s.
Do you know what the Problem could be?
thanks in advance
//Edit: Here is the code but i dont think it has anything to do with this and what do you mean with network it policies?
FileInputStream fstream = new FileInputStream(linksFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String link;
String name;
while ((link = br.readLine()) != null) {
name = br.readLine();
FileUtils.copyURLToFile(new URL(link), new File("videos/"+name+".flv"));;
System.out.println(link);
}
This isn't likely to be a Java problem.
The code you've posted actually doesn't do any IO over the network - it just determines a URL and passes it to (presumably Apache Commons') FileUtils.copyURLToFile. As usual with popular third-party libraries, if this method had a bug in it that caused slow throughput in all but the most unusual situations, it would already have been identified (and hopefully fixed).
Thus the issue is going to lie elsewhere. Do you get the expected speeds when accessing resource through normal HTTP methods (e.g. in a browser)? If not, then there's a universal problem at the OS level. Otherwise, I'd have a look at the policies on your network.
Two possible causes spring to mind:
The obvious one is some sort of traffic shaping - your network deprioritises the packets that come from your Java app (for an potentially arbitrary reason). You'd need to see hwo this is configured and look at its logs to see if this is the case.
The problem resides with DNS. If Java's using a primary server that's either blocked or incredibly slow, then it could take up to a few seconds to convert that URL to an IP address and begin the actual transfer. I had a similar problem once when a firewall was silently dropping packets to one server and it took three seconds (per lookup!) for the Java process to switch to the secondary server.
In any case, it's almost certainly not the Java code that's at fault.
The FileUtils.copyURLToFile internals uses a buffer to read.
Increasing the value of the buffer could speed up the download, but that seems not possible.
I'm trying to write an ircBot in Java for some practice. I am using this sample code as the base. I'm trying to figure out how to get it to read in text from my console so I can actually talk to people with the bot.
There's the one while loop that takes in the input from the ircserver and spits it out to console and responds to PINGs. I'm assuming I have to have another thread that takes the input from the user and then uses the same BufferedWriter to spit it out to the ircserver again but I can't get that figured out.
Any help would be awesome!
In the code you have linked to, the 'reader' and 'writer' instances, are indeed connected to respectively the ingoing and outgoing ends of the two-way socket you have established with the IRC server.
So in order to get input from the User, you do indeed new another thread which takes commands from the user in some fashion and acts upon these. The most basic model, would naturally be to use System.in for this, preferably wrapping it so that you can retrieve whole line inputs from the User, and parse these as a command.
To read whole lines from System.in you could do something like this:
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = bin.readLine()) != null) {
// Do stuff
}
You could also consider using one of the CLI libraries that is out there for Java, like JLine
If you really want to do yourself a favour, I recommend (after having used it extensively) switching to pircbot. Pircbot really is a wonderful library and will let you get an IRC bot up and running in just a few minutes. Check out some of the examples on the site, it's super easy to use.