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!

[3910 byte] By [Kreekoha] at [2007-10-3 4:00:56]
# 1

Well you don't make it any easier by not saying what it does that you don't expect, or doesn't do that you do expect, but some comments:

(a) Why is this all in one class? Much simpler to write a server class and a client class.

(b) When you close the socket, just close the buffered output stream. This will also cause it to flush.

(c) Your read loop will never exit. When you read the null you have read EOF and you must break out from the loop.

ejpa at 2007-7-14 22:00:04 > top of Java-index,Core,Core APIs...
# 2
Oh ya, sorry. Usually giving the problem when asking for a solution is a good idea. The problem is that it keeps giving me a connection refused error. But in the meantime I'll try your other suggestions, thanks.
Kreekoha at 2007-7-14 22:00:04 > top of Java-index,Core,Core APIs...
# 3
That's because there is no server port open or a firewall is blocking the inbound request to the server.
watertownjordana at 2007-7-14 22:00:04 > top of Java-index,Core,Core APIs...
# 4
That's what I thought, so i opened that port and I tried it out. It can't connect with my friend's computer, or even with my own computer if I have two instances of it running.
Kreekoha at 2007-7-14 22:00:04 > top of Java-index,Core,Core APIs...
# 5
Code appears fine, except that you have everything in one class.There are also some public methods you don't really need. Also the wait() and sWait() methods don't seem to be needed either.
watertownjordana at 2007-7-14 22:00:04 > top of Java-index,Core,Core APIs...