I/O streams, need help!

Discussion in 'Programming General' started by Shad3d-us3r, Jan 1, 2010.

I/O streams, need help!
  1. Unread #1 - Jan 1, 2010 at 5:15 PM
  2. Shad3d-us3r
    Joined:
    Jan 27, 2007
    Posts:
    88
    Referrals:
    0
    Sythe Gold:
    0

    Shad3d-us3r Member

    I/O streams, need help!

    Well, I decided to play around and make a socket based chat program I've been trying to use nothing but the Java API for the project but I can't seem to get input and output streams working. Am I doing something wrong? I know my code is pretty messed up but I just want it to work first before I optimize it. Thanks! I want the Client to be able to send messages to the Server.

    ChatServer.java
    Code:
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class ChatServer {
    	static InputStream in;
    	static Scanner sc = new Scanner(System.in);
    
    	
    	
    	public ChatServer() throws IOException{
                    
    		byte[] b = new byte[760];
    		ServerSocket s = new ServerSocket(9000);
    		System.out.println("Accepting connections");
    		s.accept();
    		System.out.println("Connection Acquired");
    		DataInputStream dis = new DataInputStream(in);
    		dis.read(b);
    		String h = sc.next();
    		if (h.equals("Close")) {
    			s.close();
    		} else {
    		System.out.println("Wat");
    		}
    		
    	}
    
    	public static void main(String[] args) {
    		try {
    		System.out.println("Server Creating");	
    		ChatServer cs = new ChatServer();
    	       
    		}catch(IOException io) {
    		System.out.println("Error");
    		}
    	}
    
    }
    ChatClient.java
    Code:
    import java.net.*;
    import java.io.*;
    import java.io.DataOutputStream;
    import java.util.Scanner;
    
    public class ChatClient {
    
    	static Scanner sc = new Scanner(System.in);
    	
    
    	public ChatClient() throws IOException, UnknownHostException{
    		
    		System.out.println("Acquiring Connection");
    		Socket s = new Socket(InetAddress.getLocalHost(), 9000);
    	
    		System.out.println("Connection made");
    		ByteArrayOutputStream bous = new ByteArrayOutputStream();
    		DataOutputStream dos = new DataOutputStream(bous);
    		dos.writeChars(sc.next());
    		/*			
    		while(s.isBound() == false) {
    		System.out.println("Isn't bound");
    		s.close();
    		}*/
    		String h = sc.next();
    		if (h.equals("Close")) {
    			s.close();
    		} else {
    		System.out.println("Wat");
    		}
    		}
    
    	public static void main(String[] args) {
    		try {
    		ChatClient cc = new ChatClient();
    		
    		} catch(IOException ie) {
    		}
    		}
    
    	}
     
  3. Unread #2 - Jan 1, 2010 at 5:54 PM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    I/O streams, need help!

    Explain the issue in detail... I may be able to help but I don't have the JDK on my moms shitty old computer... Mines broken. >_>.
     
  5. Unread #3 - Jan 1, 2010 at 7:21 PM
  6. Shad3d-us3r
    Joined:
    Jan 27, 2007
    Posts:
    88
    Referrals:
    0
    Sythe Gold:
    0

    Shad3d-us3r Member

    I/O streams, need help!

    I rewrote some of my code now the input/output streams compile and stuff, but I'm still not sure how to send bytes over the socket.


    ChatServer.java
    Code:
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class ChatServer {
    	static Scanner sc = new Scanner(System.in);
    
    	public ChatServer() throws IOException{
                    
    		byte[] b = new byte[760];
    		ServerSocket s = new ServerSocket(9000);
    		System.out.println("Accepting connections");
    		s.accept();
    		System.out.println("Connection Acquired");
    		s.accept();
    		System.out.println(s.isClosed());
    		System.out.println(s.isBound());
    		s.setSoTimeout(90000);
    }
    
    	public static void main(String[] args) {
    		try {
    		System.out.println("Server Creating");	
    		ChatServer cs = new ChatServer();
    	       
    		}catch(IOException io) {
    		System.out.println("Error");
    		}
    	}
    
    }



    ChatClient.java

    Code:
    import java.net.*;
    import java.io.*;
    import java.io.DataOutputStream;
    import java.util.Scanner;
    
    public class ChatClient {
    
    	static Scanner sc = new Scanner(System.in);
    	
    
    	public ChatClient() throws IOException, UnknownHostException{
    		
    		System.out.println("Acquiring Connection");
    		Socket s = new Socket(InetAddress.getLocalHost(), 9000);
    	
    		System.out.println("Connection made");
    		byte[] b = new byte[760];
    		byte[] j = new byte[760];
    		int test = sc.nextInt();
    		b[2] = (byte)test;
    		for (int i = 0; i < b.length; i++) {
    			OutputStream os = new ByteArrayOutputStream(b[i]);
    			os.write(b[i]);
    			System.out.print(b[i]);
    		}
    		System.out.println(" ");
    		for (int z = 0; z < j.length; z++) {
    			InputStream dis = new ByteArrayInputStream(j);
    			j[z] = (byte)dis.read();
    			System.out.print(j[z]);
    		}
    		/*			
    		while(s.isBound() == false) {
    		System.out.println("Isn't bound");
    		s.close();
    		}
    		String h = sc.next();
    		if (h.equals("Close")) {
    			s.close();
    		} else {
    		System.out.println("Wat");
    		}*/
    		}
    
    	public static void main(String[] args) {
    		try {
    		ChatClient cc = new ChatClient();
    		
    		} catch(IOException ie) {
    		}
    		}
    
    	}
    The server part works correctly, I can start the server and get two clients to join it, then I type in on one client what to send and I want it to receive the byte on the other client. Sort of like a chat program but with bytes.

    If you do have the solution for this, I'd rather you not say it unless you plan on explaining what you did, I don't want to copy code here.
     
  7. Unread #4 - Jan 1, 2010 at 10:22 PM
  8. Shad3d-us3r
    Joined:
    Jan 27, 2007
    Posts:
    88
    Referrals:
    0
    Sythe Gold:
    0

    Shad3d-us3r Member

    I/O streams, need help!

    Problem solved! After 6 hours of trying everything. If you guys want the solution I can post it up(I don't comment my code though so it might be hard to understand).
     
< Good book for C++ | Global Hotkeys and Mouse-Click Forging >

Users viewing this thread
1 guest


 
 
Adblock breaks this site