• Log in or sign up for an account here: https://www.sythe.org/login

I/O streams, need help!

Shad3d-us3r

Member
Joined
Jan 27, 2007
Sythe Gold
0
Messages
88
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) {
		}
		}

	}
 
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. >_>.
 
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.
 
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).
 
Runescape Gamblingrunescape gambling
Back
Top