Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
I/O streams, need help!
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-01-2010, 10:15 PM
Shad3d-us3r's Avatar
Member
 
Join Date: Jan 2007
Posts: 88
Default 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) {
		}
		}

	}
__________________
Vouch List:
Quote:
Originally Posted by yoyotw666 View Post
VOUCH..got me 55-58 attk fast!!!
Quote:
Originally Posted by teh_pwn33r View Post
MEGA PINK!!\ Vouch for shad3d us3r made me mah pwning sig!\
Quote:
Originally Posted by kurtika View Post
Huge Vouche For Shad3d-us3r Trained My Str For Free
100% Legit
Quote:
Originally Posted by my white View Post
VOUCH Got 1-16 in about 30-40 mins but asked for 30 but had unfinished busniess. Thanks!
Quote:
Originally Posted by BFlyBoy View Post
shad3d Us3r - Nice And Friendly Trader Reccomended
Quote:
Originally Posted by tozbror View Post
GOOD VOUCH FOR SHAD the trade went smooth and good swaped skiller for main
Reply With Quote
  #2  
Old 01-01-2010, 10:54 PM
Legend
Java Programmers Pirate
 
Join Date: Jan 2007
Posts: 10,882
Spam Forum Participant <3 n4n0
Default Re: 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. >_>.
__________________
PM me with any issues.

I do not MM or trade anything.
Anyone claiming to be me in a trade or MM is an imposter.
Reply With Quote
  #3  
Old 01-02-2010, 12:21 AM
Shad3d-us3r's Avatar
Member
 
Join Date: Jan 2007
Posts: 88
Default Re: 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.
__________________
Vouch List:
Quote:
Originally Posted by yoyotw666 View Post
VOUCH..got me 55-58 attk fast!!!
Quote:
Originally Posted by teh_pwn33r View Post
MEGA PINK!!\ Vouch for shad3d us3r made me mah pwning sig!\
Quote:
Originally Posted by kurtika View Post
Huge Vouche For Shad3d-us3r Trained My Str For Free
100% Legit
Quote:
Originally Posted by my white View Post
VOUCH Got 1-16 in about 30-40 mins but asked for 30 but had unfinished busniess. Thanks!
Quote:
Originally Posted by BFlyBoy View Post
shad3d Us3r - Nice And Friendly Trader Reccomended
Quote:
Originally Posted by tozbror View Post
GOOD VOUCH FOR SHAD the trade went smooth and good swaped skiller for main
Reply With Quote
  #4  
Old 01-02-2010, 03:22 AM
Shad3d-us3r's Avatar
Member
 
Join Date: Jan 2007
Posts: 88
Default Re: 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).
__________________
Vouch List:
Quote:
Originally Posted by yoyotw666 View Post
VOUCH..got me 55-58 attk fast!!!
Quote:
Originally Posted by teh_pwn33r View Post
MEGA PINK!!\ Vouch for shad3d us3r made me mah pwning sig!\
Quote:
Originally Posted by kurtika View Post
Huge Vouche For Shad3d-us3r Trained My Str For Free
100% Legit
Quote:
Originally Posted by my white View Post
VOUCH Got 1-16 in about 30-40 mins but asked for 30 but had unfinished busniess. Thanks!
Quote:
Originally Posted by BFlyBoy View Post
shad3d Us3r - Nice And Friendly Trader Reccomended
Quote:
Originally Posted by tozbror View Post
GOOD VOUCH FOR SHAD the trade went smooth and good swaped skiller for main
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 08:52 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1