Hey,
ich versuche mich grade in die Server/Client-Geschichte in Java einzuarbeiten, hatte kurzweilig auch ein eigenen Client geschrieben, der funktioniert hatte (nicht für den KEmu). Nun hatte ich versucht mit der Source, die gepostet wurde einen eigenen Client zu bauen.
Client -> Server geht. (Handshake kommt am Server an)
Server -> Client geht nicht. Vom Server aus wird etwas gesendet, kommt jedoch nicht am Client an. Als Exception im run() wird immer nur "InputStream is null!" angegeben.
Mal meine ChatApplet.java, sowie die Server.java und Client.java:
Chatapplet.java
|
Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mhclient;
import java.applet.Applet;
import java.applet.AppletContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author Marc
*/
class ChatApplet extends Applet implements Runnable{
private static ChatApplet instance;
static {
instance = new ChatApplet();
}
private String server_host;
private int server_port;
private AppletContext applet_context;
private String applet_location;
private String java_version;
private String java_software;
private Socket server_socket;
private InputStream server_socket_input;
private OutputStream server_socket_output;
private final String client_version = "V0.1aaa";
private boolean server_connected;
public ChatApplet() {
this("", 0);
}
public ChatApplet(String server_host, int server_port) {
this.server_host = server_host;
this.server_port = server_port;
}
protected void initApplet() {
System.out.println("BananaClient "+this.client_version);
this.applet_context = getAppletContext();
this.applet_location = getDocumentBase().toString();
this.java_version = System.getProperty("java.version");
this.java_software = System.getProperty("java.vm.vendor").toLowerCase();
try {
this.server_port = 2710;
} catch (Exception localException10) {
System.err.println("ERROR: No port!");
}
if(this.server_host == null) {
this.server_host = "127.0.0.1";
}
}
protected void startApplet() {
try {
this.server_socket = new Socket("localhost", 2710);
this.server_socket_input = this.server_socket.getInputStream();
this.server_socket_output = this.server_socket.getOutputStream();
this.server_connected = true;
System.err.println(this.server_socket.toString());
} catch(NoRouteToHostException error_firewall) {
this.server_connected = false;
System.out.println("Server nicht erreichbar. (Firewall)" + error_firewall);
} catch(ConnectException error_maintenance) {
this.server_connected = false;
System.out.println("Server wird zur Zeit gewartet. \nNeuer Versuch in " + error_maintenance);
} catch (UnknownHostException error_host) {
this.server_connected = false;
System.out.println("Server nicht erreichbar. (Host not Found)" + error_host);
} catch (IOException error_io) {
this.server_connected = false;
System.out.println("Verbindungsfehler. (IO)" + error_io);
}
if((java_version == null) || (java_version.length() == 0)) {
java_version = "unknown";
}
if(this.server_connected) {
new Thread(instance).start();
this.firstshake(PacketCreator.startConnection());
send(PacketCreator.setHandshake(this.client_version));
}
}
public static ChatApplet get() {
return instance;
}
@Override
public void run() {
while(true){
try{
InputStream test = server_socket_input;
byte[] buffer = Protocol.decode(server_socket_input);
String packet = new String(buffer);
String[] tokens = packet.split("\0");
String opcode = tokens[0];
System.err.println(packet);
System.err.println("[ >>> ] " + packet.replace("\0", "\\0"));
}catch(Exception e){
if(e.getMessage() != "InputStream is null!"){
System.out.println(e.getMessage());
}
}
}
}
private void firstshake(String message) {
if(this.server_socket != null && server_socket.isConnected()) {
try {
this.server_socket_output.write(message.getBytes("UTF8"));
System.err.println("[ >>> ] " + message.replace("\0", "\\0"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void send(String message) {
if(this.server_socket != null && server_socket.isConnected()) {
try {
this.server_socket_output.write(Protocol.encode(message.getBytes("UTF8")));
System.err.println("[ >>> ] " + message.replace("\0", "\\0"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
|
Server.java
|
Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package global;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import tools.database.ConnectionPool;
import tools.database.PoolConnection;
/**
*
* @author Flav
*/
public class Server {
private final static Server instance;
private final Map<String, Client> clients;
private Client butler;
static {
instance = new Server();
}
public Server() {
clients = new HashMap<String, Client>();
butler = new Client(null);
}
public static Server get() {
return instance;
}
public Client getButler() {
return butler;
}
public Client getClient(String name) {
synchronized (clients) {
return clients.get(name.toLowerCase());
}
}
public Collection<Client> getClients() {
synchronized (clients) {
return clients.values();
}
}
public void removeClient(String name) {
synchronized (clients) {
clients.remove(name.toLowerCase());
}
}
private void loadConfigs() {
PoolConnection pcon = ConnectionPool.getConnection();
Statement stmt = null;
try {
Connection con = pcon.connect();
stmt = con.createStatement();
System.out.println("Loading butler");
ResultSet rs = stmt.executeQuery("SELECT `name` FROM `accounts` ORDER BY `id` LIMIT 1");
rs.next();
//butler.login(rs.getString("name"));
rs.close();
System.out.println("Loading channel styles");
rs = stmt.executeQuery("SELECT * FROM `channelstyles`");
// Map<Integer, ChannelStyle> channelStyles = new HashMap<Integer, ChannelStyle>();
while (rs.next()) {
//channelStyles.put(rs.getInt("id"), new ChannelStyle(rs));
}
rs.close();
System.out.println("Loading channels");
rs = stmt.executeQuery("SELECT * FROM `channels`");
while (rs.next()) {
//Channel channel = new Channel(rs, channelStyles.get(rs.getInt("style")));
//butler.joinChannel(channel);
//channel.addClient(butler);
//channels.put(rs.getString("name").toLowerCase(), channel);
}
rs.close();
System.out.println("Loading smileys");
rs = stmt.executeQuery("SELECT `code`, `replacement` FROM `smileys`");
while (rs.next()) {
//smileys.put(rs.getString("code"), rs.getString("replacement"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
}
}
pcon.close();
}
}
private void listen(int port) {
try {
ServerSocket listener = new ServerSocket(port);
System.out.println(String.format("Listening on port %s", port));
while (true) {
Socket socket = listener.accept();
clients.put(null, butler);
new SessionHandler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 2710; // default
}
instance.loadConfigs();
instance.listen(port);
}
}
|
Und zuletzt die Client.java
|
Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package global;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Marc
*/
public class Client {
private final List<Channel> channels;
private final Socket socket;
private OutputStream out;
private String butler = "Chuck";
private String appletVersion;
public Client(Socket socket) {
channels = new ArrayList<Channel>();
if(socket!=null){
System.out.println(socket.toString());
}
this.socket = socket;
if (socket == null) {
return;
}
try {
out = socket.getOutputStream();
} catch (IOException e) {
}
}
public String getName() {
return butler;
}
public void send(String message) {
if (socket != null && socket.isConnected()) {
try {
out.write(Protocol.encode(message.getBytes("UTF8")));
System.err.println("[ >>> ] "+ message.replace("\0", "\\0"));
System.out.println("Send to:"+ this.socket.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
void setAppletVersion(String version) {
this.appletVersion = version;
}
}
|