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.