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

