Socket confusion...
I'm trying to make a class that will send and recieve strings over a TCP/IP connection. Everything I've read says that I'm doing it the right way. Here's the code I'm trying to use:
import java.net.*;
import java.io.*;
import java.lang.*;
import java.net.InetAddress;
public class MultiplayerConnection {
int ssc = -1;
ServerSocket sersock = null;
Socket sock = null;
int PORT = 1028;
InetAddress ip, ip2, host;
BufferedReader br;
DataOutputStream os;
boolean setup = false;
PrintStream ps;
String name, hostip;
public MultiplayerConnection() {
ssc = 0;
server();
}
public MultiplayerConnection(String inet) {
hostip = inet;
client();
}
public void server() {
try {
sersock = new ServerSocket(1028);
System.out.println("Server Started :" + sersock);
connectWait();
System.out.println("Connection Established");
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void connectWait(){
try {
sock = sersock.accept();
System.out.println("Client Connected :" + sock);
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
ps = new PrintStream(sock.getOutputStream());
} catch(SocketException se) {
System.out.println("Server Socket problem: " + se.getMessage());
} catch(IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
public void client() {
try {
sock = new Socket(hostip, 1028);
System.out.println("Client Started: " + sock);
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
ps = new PrintStream(sock.getOutputStream());
ps.print("Works");
} catch(SocketException se) {
System.out.println("Socket Exception " + se.getMessage());
} catch(IOException ioe) {
System.out.println("IO Exception " + ioe.getMessage());
}
}
public String getIP() {
return ip.toString();
}
public void closeConnection() {
try {
if(ssc == 0) {
sersock.close();
sock.close();
} else
sock.close();
System.out.println("Connection Closed");
} catch(IOException e) {
System.out.println("Connection not closed");
System.out.println("IO Exception " + e.getMessage());
}
}
public InetAddress getHostIP() {
return host;
}
public InetAddress getServerAddress() {
try {
ip = sersock.getInetAddress();
} catch(Exception e) {
System.out.println("Exception - " + e.getMessage());
}
return ip;
}
public static byte[] parseIP(String ipadd) {
int start = 0, next = ipadd.indexOf('.');
byte[] addr = new byte[4];
String temp = null;
for(int count = 0; count <= 3; count++) {
System.out.println(""+start+" "+next);
if(count < 3)
temp = ipadd.substring(start, next);
if(count == 3)
temp = ipadd.substring(start, ipadd.length());
System.out.println(temp);
addr[count] = (byte)Integer.parseInt(temp);
if(count < 3){
start = next+1;
next = ipadd.indexOf('.', start+1);
} else {
start = next+1;
next = ipadd.indexOf('.', start+1);
}
}
return addr;
}
public String sWait() {
int i = 0;
String s = null;
while(i != 1) {
try {
if(br.readLine().equals(null)) {
} else {
s = br.readLine();
i++;
}
} catch(IOException e) {
System.out.println("Exception - " + e.getMessage());
}
}
if(s == null)
return "WTF?";
else
return s;
}
}
Can anyone help me get this working? I'd really appreciate it!

