My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 10 attachment: Client.java (3.9 KB)

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
package net.dreamersnet.ChatServer;
import java.io.*;
import java.net.*;

/**
* Client - a class to connect to the simple chat server I'm designing.
* @author Waterlgndx
*
*/
public class Client {
//You may want to change this!!!
final static String DEFAULT_HOST = "home.dreamersnet.net";
final static String DEFAULT_NAME = "Anonymous";
static int attempts = 0;
final static int DEFAULT_PORT = 4444;
//TODO: add a way to configure this ( Config file? )
static String name;
static String host;
static int port;
static Socket socket;
static int nreq = 0;
static Client cli = new Client();
static AppWindow app = new AppWindow(cli);
static Thread conInThread;
static boolean quit = false;

Client () {
}


public static void main(String[] args) {
if (args.length>0)
name = args[0];
if (args.length==2) {
host = args[1];
}

if (args.length==0) {
name = app.askString("Set Name: ", "Name");
host = app.askString("Connect to host? ( Press <enter> for home.dreamersnet.net ) ", "Connect");
}

if (name.length()==0) {
name= DEFAULT_NAME;
}

if (host.length()==0) {
host= DEFAULT_HOST;
}

AppWindow.run();
if ((args.length>=1) || (name.length()>1)) {
try {
socket = new Socket(host, DEFAULT_PORT);
socket.setKeepAlive(true);
cli.sendRaw(name + " has connected!");
while (!quit)
{

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String newLine = bufferedReader.readLine().trim();
if (newLine.startsWith("@#!set name")) {
String[] words = newLine.split(" ",3); // .split ==> ,3) means 3 positions in array are created.
name = words[2];
continue;
} else {
app.println(newLine);
}
}
} catch (SocketException sexc) {
repair();
System.out.println("main exception has occured : " + sexc);
app.println("main exception has occured : " + sexc);
sexc.printStackTrace();
System.exit(1);
} catch (Exception e) {
System.out.println("main exception has occured : " + e);
e.printStackTrace();
}
}
}

private static void repair() {
if (attempts <= 10) {
System.out.println("Socket \n Closed: " + socket.isClosed() + "\n InputFailure: " + socket.isInputShutdown() + "\n Connected:" + socket.isConnected());
System.out.println("System exiting...");
String[] send = (name + " "+ host).split(" ");
attempts++;
main(send);
} else {
app.println("Number of attempts exceeds the limit!");
System.exit(1);
}

}

public void sendMessage(String msg)
{
//send the message
try {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println(name + " " + msg); // send it!
printWriter.flush();
} catch (IOException e) {
app.println("Client SendMessage Error occured: " + e);
e.printStackTrace();
System.exit(1);
}
}

public void sendRaw(String raw)
{
try {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println(raw); // send it!
printWriter.flush();
} catch (IOException e) {
app.println("Client SendMessage Error occured: " + e);
e.printStackTrace();
System.exit(1);
}
}

public void sendCommand(String command) {
if (command.compareToIgnoreCase("quit")==0) {
try {
sendRaw("!#@" + name + " " + command);
quit = true;
socket.close();
} catch (IOException e) {
app.println("ClientCommand Error occured: " + e);
e.printStackTrace();
System.exit(1);
}
}
String[] words=command.split(" ", 2);
if (words.length <= 1) {
sendRaw("!#@" + name + " " + command);
return;
}
else {
sendRaw("!#@" + name + " " + words[0] + " " + words[1]);
}
}
}
Powered by Google Project Hosting