@Hamsterman, and others;
Here are my rectangles, they actually represent functions! They will represent classes, Im going back to learning c++ again because its more familiar, but getting the concepts down in java before approaching them in c++ will help me undersand the documentation better.
My aim for this code was to make a multi-thread network handling server thing that worked with a smooth protocol, I did make one with a tutorial but to prove to myself I understood it I tried to make my own design, thats why i wanted to plan it with rectangles, I also wanted a way of typing commands to change things in, I was hoping it would give me some insight for learning bash, (thats an idea making a faux bash type scripting program for practice--thats what I will practice c++ with again later)
any way heres code, Im not sure if it works but I sure get a lot of errors and theres no close function yet
heres the link to my rectangles: www.dropbox.com/s/8eycdda0gpegl7i/rectangles%20as%20functions.bmp
as for my code
message handling bits:
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
|
public class messagestuff {
private Scanner speakline = new Scanner(System.in);
public boolean listening = true;
static String[] namearray = new String[10];
public String[] messagearray = new String[3];
public void commandhandler(String cmdstring){
if(cmdstring.contains("endlisten101")){
listening = false;
msgeoutput("Stoping listening");
}
}
public void msgeoutput (String speak){
System.out.println(speak);
}
public void msginput (){
String line;
line = ": " + speakline.nextLine();
stringmanager(line);
}
public void programupdate (String msg){
String line;
line = "update101 "+ msg;
stringmanager(line);
}
public synchronized void stringmanager (String X){
String inputstring = X;
String message;
Integer inputcode = null;
if (inputstring.contains("update101")){ //handle system update messages
message = inputstring.replace("update101","System: ");
msgeoutput(message);
}
if(inputstring.contains("command101")){ //handle program commands
commandhandler(inputstring);
}
for (int a = 0; a <namearray.length; a++){ //u always mess < up
//if (namearray=null) ... this was an early bug
if (inputstring.contains(namearray[a])){
messagearrayfunction(inputstring);
msgeoutput(namearray[a]+": " + inputstring);
}
}
}
public void messagearrayfunction (String s){
messagearray[0]= s;
}
}
|
the server socket:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class getconnected extends messagestuff{
public synchronized void connectionaccepter (){
while (listening){
try {
ServerSocket connector = new ServerSocket(7001,10);
programupdate("Server socket OK");
while (listening){
new threadmaybe(connector.accept()).run();
}
} catch (IOException e) {
programupdate("Error connecting to server socket;\n"+e.toString());
}
}
}
}
|
the thread running class
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
|
public class threadmaybe extends messagestuff implements Runnable {
PrintWriter outbuf;
BufferedReader inbuf;
public String name;
public Socket threadsock = new Socket();
static Integer nameindex = 0;
private String ding;
public threadmaybe (Socket socket){
this.threadsock = socket;
programupdate("Socket in thread connected to server socket");
try {
outbuf = new PrintWriter(threadsock.getOutputStream(),true);
inbuf = new BufferedReader(new InputStreamReader(threadsock.getInputStream()));
name = inbuf.readLine();
namearray[nameindex] = name;
nameindex++;
} catch (IOException e) {
programupdate("Error connecting streams to Client: " + e.toString());
}
}
public synchronized void run() {
outbuf.println("Connected to server");
while(threadsock.isConnected()){
outbuf.println(messagearray[0]);
messagearray[0]="";
try {
msgeoutput(inbuf.readLine());
} catch (IOException e) {
programupdate("input fuckup" + e.toString());
}
}
programupdate("name" + "has left the session");
}
}
|
and the main bit just so you can see how I call this sorry program
1 2 3 4 5 6 7
|
public class mainpart {
public static void main(String[] args) {
getconnected listener = new getconnected();
listener.connectionaccepter();
}
}
|
c++ is more exciting than java, I will go back once I feel I can make someting that does what I want I will go back to improving c++, I actually never learned to use parameterized classes and such, that was tottaly the next step, just I moved to other languages
EDIT: oh yeah theres bugs galore in it, i still never got it to work like clokwork, I dont think i ever got anything worling like clockwork, I dont think any programmer in the 21st centuary will (unless hes a clockprogrammer)