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 2 of 2

Thread: Java Client

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Client

    My server works perfectly fine, I understand the basic function of it and using telnet with it works perfectly. My problem is with the client, I can't figure out how to make it receive messages. Here's my client code, I can post the server code if need be.

    import java.awt.*;
    import java.applet.Applet;
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
    import javax.swing.Timer;
    import javax.swing.*;
    import java.applet.AudioClip;
    import java.net.*;
     
    public class JClient {
      private  Graphics BufferGraphics;
      private  Image offscreen;
      private  String currentScreen;
     
      //NETWORKING VARIABLES
       int port;
       String servIP;
       Socket mySocket;
       SocketAddress server;
       private PrintWriter toServer;
       private BufferedReader fromServer;
       private ServerListener receiver;
     
      //WINDOW VARIABLES
       JFrame frame;
       extendedPanel panel;
     
    	public static void main(String[] args) throws IOException{
     
    		new JClient();
    	}
    	public JClient(){ 
    		currentScreen="mainmenu";
    		frame = new JFrame("I FUCKING HATE SWING WAHHHHHHHHHHH");
    		panel = new extendedPanel(new ImageIcon("GFX/GUI/"+currentScreen+".png"), 0, 0, 800, 600)
    		{
    			public void mousePressed(MouseEvent e)
    			{	
    				toServer.print("CREATEACC ninja test");
    				toServer.flush();
    				//System.err.println("PRESSED");
    			}
    		};
    		Container content = frame.getContentPane();
    		content.setLayout(null);
    		content.add(panel);
    		frame.setDefaultCloseOperation(3);
    		frame.setSize(800, 600);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		init();
    		makeContact();
      }
     
    	private void init(){
     
    		//SERVER IP INFORMATION
    		servIP="127.0.0.1"; port=9999;
     
    		currentScreen="mainmenu";
     
    	}
    	private void makeContact(){
    		System.err.println("Making contact...");
    		try{
    			mySocket=new Socket(servIP,port);
    			System.out.println("Connected on "+servIP);
    			fromServer=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
    			new ServerListener(this,fromServer);
    			toServer=new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()));
     
     
    		}catch(Exception e){}
     
    	}
    	/////////////////////////////////////////////////////////////////
    	/////////////////////////////////////////////////////////////////
    	///////// HANDLING MOUSE AND KEYBOARD INPUT /////////////////////
    	/////////////////////////////////////////////////////////////////
    	/////////////////////////////////////////////////////////////////   
     
    }//END CLIENT
     
     
    class ServerListener extends Thread{
    	JClient client;
    	BufferedReader in;
     
    	public ServerListener(JClient c,BufferedReader i){
    		client=c;
    		in=i;
    		start();
    	}
    	public void run(){
    		String msg="a";
    			System.out.println("thread started");
    		try{
    			System.out.println("inside the try");
    			while(msg!=null){
    				System.out.println("inside the loop");
    				System.out.println(""+msg);
    				msg=in.readLine();
    			}
    			System.out.println("done with loop");
    		}catch(Exception e){}
    	}
    }//END SERVERLISTENER
     
    class extendedPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener
    {
    	JPanel panel;
    	ImageIcon pic;
    	Image image;
    	int x, y, w, h;
    	int mX, mY;
    	boolean ms_pressed = false;
     
    	public extendedPanel(ImageIcon p, int locX, int locY, int sizeW, int sizeH)
    	{
    		pic = p;
    		x = locX;
    		y = locY;
    		w = sizeW;
    		h = sizeH;
     
    		init();
    	}	
     
    	private void init()
    	{
    		image = pic.getImage();
    		setOpaque(true);  //indicates that this is indeed the bottom layer
    		setLocation(x,y);
    		setSize(w,h);
    		addMouseListener(this);
    	}
     
    	public int getMouseX()
    	{
    		return mX;
    	}
     
    	public int getMouseY()
    	{
    		return mY;
    	}
     
    	public void paintComponent(Graphics g) 
    	{
     
    		Dimension dim = getSize();
         	int x, y;
     
    		// ensure proper background handling
        	if( isOpaque() ) { super.paintComponent( g ); }
     
    		// redraw image horizontal/vertical until panel is filled
         	for( y = 0; y < dim.height; y += h )
         	{
          		for( x = 0; x < dim.width; x += w )
    	     		pic.paintIcon( this, g, x, y );
         	}
     
    	}
     
    	public boolean getMouse()
    	{
    		return ms_pressed;
    	}
     
     
    	public void mouseClicked(MouseEvent e)
    	{
    		//ms_clicked = true;
    	}
     
    	public void mouseDragged(MouseEvent e)
    	{}
    	public void mouseMoved(MouseEvent e)
    	{}
    	public void mouseReleased(MouseEvent e)
    	{
    		//ms_pressed = false;
    	}
    	public void mouseExited(MouseEvent e)
    	{}
    	public void mouseEntered(MouseEvent e)
    	{}
    	public void mousePressed(MouseEvent e)
    	{
    		//ms_pressed = true;
    	}
    	public void keyPressed(KeyEvent e)
        {
        /* DIFFERENT KEYS:::
                case KeyEvent.VK_UP:
                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_SPACE:
                case KeyEvent.VK_ESCAPE:
                case KeyEvent.VK_F1:
        */
        }
        public void keyReleased(KeyEvent e)
        {}
        public void keyTyped(KeyEvent e)
        {}
     
     
     
    }

    any help would be greatly appreciated!
    Last edited by newsomjk; March 31st, 2010 at 10:45 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Client

    scratch that. figured out the problem. not sure how to delete this thread.

Similar Threads

  1. Simple Java SOAP client
    By andy in forum Java Networking
    Replies: 3
    Last Post: December 16th, 2010, 04:05 PM
  2. Replies: 2
    Last Post: November 19th, 2009, 11:55 PM
  3. RMI over SSL Client application
    By boilerchicken in forum Java Networking
    Replies: 0
    Last Post: November 10th, 2009, 07:52 AM
  4. [SOLVED] web client
    By 5723 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: June 10th, 2009, 04:44 PM
  5. How to write switch statement inside if statement?
    By Rezz in forum Loops & Control Statements
    Replies: 6
    Last Post: June 11th, 2008, 11:27 AM