Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: I am trying to make a messenger program!

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I am trying to make a messenger program!

    I got to do a kind of Messenger in Java. Well .. it will not have to do something with a server or making accounts. Will simply be a program which will be used to communicate between 2 computers into a network, by setting the IP of the computers that communicate. I think to make a window with a smaller text area where each user writes the messages and another text area where each message will appear after the user touches a SEND key. I am asking if there are any packages or classes which will help me to make the second text area easier. I already seen that i have JavaFrameWork to broadcast an audio/video file on this messenger. Yeah, i have to add video/audio options too but if i manage to make it work with sending message, it will be much easier to finish it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    Are you going to write both a server and client code? There are dozens of sample programs from students on the forums.

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    basically on each computer should run 2 Threads:
    one of them sends the message having the role of server and the other one receives the message having the role of client!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    Are you saying that Each PC is a server that other PCs can connect to directly. No master server.
    How do the PCs get the IP addresses of the other PCs?

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    So far, i manged to make it work fine with sending text message. Now i want to make my messenger to send a file from one user to the other. But i do not manage to make 2 simple sockets to send one file to another. I have found one already done...i changed it a little and seems it works very strage!
    Here is the code for the socket sending the file

     
    package testServerFisier;
     
    import java.io.*;
    import java.io.IOException;
    import java.net.*;
     
    public class TestFisier
     
    {
    	public static void main (String [] args ) throws IOException
    	{
    	    // create socket
    	    ServerSocket servsock = new ServerSocket(8555);
    	    while (true)
    	    {
    	      System.out.println("Waiting...");
     
    	      Socket sock = servsock.accept();
    	      System.out.println("Accepted connection : " + sock);
     
    	      // sendfile
    	      File myFile = new File ("C:\\Users\\nikipiulitza\\Desktop\\poze\\kazi1.bmp");
    	      byte [] mybytearray  = new byte [(int)myFile.length()];
    	      FileInputStream fis = new FileInputStream(myFile);
    	      BufferedInputStream bis = new BufferedInputStream(fis);
    	      bis.read(mybytearray,0,mybytearray.length);
    	      OutputStream os = sock.getOutputStream();
    	      System.out.println("Sending...");
    	      os.write(mybytearray,0,mybytearray.length);
    	      os.flush();
    	      sock.close();
    	      }
    	}
     
    }



    And here is the code for the socket that receives the file :

    package testPrimireFisier;
     
    import java.io.*;
    import java.io.IOException;
    import java.net.*;
    public class TestClientFisier
    {
    	  public static void main (String [] args ) throws IOException
     
    	  {
        int filesize=77507; // filesize temporary hardcoded
     
        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        // localhost for testing
        Socket sock = new Socket("192.168.0.100",8555);
        System.out.println("Connecting...");
     
        // receive file
        byte [] mybytearray  = new byte [filesize];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("C:\\Users\\nikipiulitza\\Desktop\\poze\\kazi2.bmp");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;
     
        // thanks to A. Cádiz for the bug fix
        do {                                                      // HERE is it where it stucks
           bytesRead =
              is.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
     
        bos.write(mybytearray, 0 , current);
        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        bos.close();
        sock.close();
      }
     
    }

    Looke like the server sends the file properly, but i detected at it something that may be the source of the problem :
    At the line with

    System.out.println("Accepted connection : " + sock);
    I get

    Accepted connection : Socket[addr=/192.168.0.100,port=55403,localport=8555]
    So, the IP is correctly, the one i have written at the client's socket constructor. But here, i got a port that has changed ! I am not sure, but i think that the port of the client has changed ! Why ? Can it be a problem that i run both programs on same computer ? On the messenger itself, i use Datagram sockets and when i connect to my computer, it works with sending text messages. Any clue where and how can i find why the port is changing ?
    I modified these programs little more and turned them into Threads, and added them on my messenger code, and seems its same thing which does not allow the files to be sent.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    its same thing which does not allow the files to be sent.
    What is written to the output file? Is there a problem with it?

    The code basically works for me when I use localhost.

  7. #7
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    Yeah, you are right. I think it cannot send a .bmp file properly, or maybe it cannot crate a bytearray that large, as the bmp file dimension. I tried to send a .txt file which is smaler and it worked. Now i have one more problem. I used the code of these into Thread classes in my messenger code. But i have problems with socket receiving the file. I start the thread which receives the file inside the thread which usually receives the messenger's text. When one user chooses to send a file, the thread which sends messages sends a message containing the word FILE+dimension of the file+ the name of the file. And then, the thread receiving this message will know to start the thread with the socked which receive the file. I used a lot of println and am sure that the thread which receive the file is starting properly (maybe it is wrong that i start a thread inside another thread ? ). but seems that

    do { // HERE is it where it stucks
    bytesRead =
    is.read(mybytearray, current, (mybytearray.length-current));
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    does only receive 0 value, and the do-while bubble does not end .
    Any clue why it may happen like that ? i tried to send same .txt file which worked on the programs i shown upper, and the socket receiving the file creates the .txt file, but does not receive any byte to write to it .

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    Your code has a hard coded filesize variable. Is the size correct for the file you are trying to send?

  9. #9
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    Yeah, that works properly! The size of the file is sent to the main serversocket of my messenger, inside the String containing the word FILE. I have "cut " the String and got the size from that String and used to for the constructor of the Thread which receive the file. I also used println for that and when the thread runs, the size of the bytearray is correctly set.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    If it works properly, please explain the problem.

  11. #11
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    Well , seems there is something wrong with Threads that send/receive the file.
    I made some classes separated from the messenger project, and i discovered that they actually have a problem (not the way i used them in the main project).

    Here is the Thread that is sending the file :

    package testServerFisier;
    import java.io.*;
    import java.net.*;
     
    public class ThreadSendFisier extends Thread
    {
     
    	String url;
     
    	public ThreadSendFisier(String url ) 
    	{
    		this.url =url;
     
    	}
     
     
    	public void run ()
    	{
     
     
     
    		try
    		{
    			  ServerSocket servsock = new ServerSocket(8221);
    			    while (true) 
    			    {
    			      System.out.println("Waiting...");
     
    			      Socket sock = servsock.accept();
    			      System.out.println("Accepted connection : " + sock);
     
    			      // sendfile
    			      File myFile = new File (url);
    			      byte [] mybytearray  = new byte [(int)myFile.length()];
    			      System.out.println((int)myFile.length());
     
    			      FileInputStream fis = new FileInputStream(myFile);
    			      BufferedInputStream bis = new BufferedInputStream(fis);
    			      bis.read(mybytearray,0,mybytearray.length);
    			      OutputStream os = sock.getOutputStream();
     
    			      System.out.println("Dimensiunea array la trimitere: " + mybytearray.length);
    			      os.write(mybytearray,0,mybytearray.length);
     
    			      os.flush();
    			      sock.close();
     
     
    			    }
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}	
    }
     
       // this is the class with the main method : 
     
    package testServerFisier;
     
    public class TestThreadSend 
    {
    	public static void main(String[] args)
    	{
    		new ThreadSendFisier("C:\\Users\\nikipiulitza\\Desktop\\poze\\fisierSend.txt").start();
    	}
     
    }


    And there is the Thread which receives the file:

    package testServerFisier;
    import java.io.*;
    import java.net.*;
     
    import mainPack.FereastraPrincipala;
     
     
     
    public class ThreadFisierPrimire extends Thread
     
    {
     
     
    	String url;
    	int filesize;
     
     
    	ThreadFisierPrimire( String url, int filesize)
    	{
     
    		this.url = url;
    		this.filesize = filesize;
    	}
     
     
    	public void run()
    	{
     
    		try
    		{
     
     
    		    long start = System.currentTimeMillis();
    		    int bytesRead;
    		    int current = 0;
    		    // localhost for testing
    		    Socket sock = new Socket("192.168.0.100",8221);
    		    System.out.println("Connecting...");
    		    System.out.println("Socket primire: "+sock);
    		    // receive file
     
    		    byte [] mybytearray  = new byte [filesize];
     
    		    InputStream is = sock.getInputStream();
    		    FileOutputStream fos = new FileOutputStream(url);
    		    BufferedOutputStream bos = new BufferedOutputStream(fos);
    		    bytesRead = is.read(mybytearray,0,mybytearray.length);
    		    current = bytesRead;
     
    		    System.out.println(sock.toString());
     
    		       do {
     
    		    	   bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
     
    		       		} while(bytesRead > -1);
     
    		    bos.write(mybytearray, 0 , current);
    		    bos.flush();
    		    long end = System.currentTimeMillis();
    		    System.out.println(end-start);
    		    bos.close();
    		    sock.close();
    			System.out.println("File Received");
    			this.notifyAll();
    		}
     
    	    catch (Exception e)
    	    {
     
    	    	e.printStackTrace();
     
    	    }
    	}
     
    }
     
       // and the class with the main method using this thread:
     
    package testServerFisier;
     
    public class TestThread
    {
    	public static void main(String[] args)
    	{
    		new ThreadFisierPrimire("C:\\Users\\nikipiulitza\\Desktop\\poze\\fisierSend2.txt",17).start();
     
    	}
     
    }


    I think I have noticed where is something going wrong, it happens at the thread which receives the file :
    do {

    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));

    } while(bytesRead > -1);
    Here, if i println bytesRead , i only get 0 and the bubble does not end.
    But sadly, i do not understand why .
    Is there anything wrong if i use these sockets inside of some Threads ? There code is nearly same as of the first programs which have the sockets build and running in the main method. But these do not work as the others did with same small .txt file.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    Add some printlns in the loop to print out the values of bytesRead and current to see what the code is doing. Compare to the value of filesize.

    Also you need to read the API doc for the read() method to be sure you understand how to use it.

  13. #13
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I am trying to make a messenger program!

    Hah ...finally i managed make it work ....i should have not changed the filesize variable to the size of the file....because it makes that bytearray length and current to have same value...so that is why i was getting 0 at each run of the bubble !

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: I am trying to make a messenger program!

    Your code is too simple minded. It needs to read some bytes in and write those bytes out in a loop until there are no more bytes to be read.

Similar Threads

  1. Trying to make a Car program and I can't seem to get it to compile
    By Necroshade in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2011, 06:45 PM
  2. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 06:44 AM
  3. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 09:31 AM
  4. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 06:09 PM
  5. Java Messenger Program?
    By MysticDeath in forum Java Networking
    Replies: 1
    Last Post: September 8th, 2009, 11:25 PM