Browse Source

WIP add TCP server

master
DemiSel 3 years ago
parent
commit
89b43cb4fb
4 changed files with 79 additions and 5 deletions
  1. 1
    0
      .idea/gradle.xml
  2. 6
    0
      .idea/vcs.xml
  3. 1
    1
      src/main/java/CuisineHTTPServer.java
  4. 71
    4
      src/main/java/CuisineTCPServer.java

+ 1
- 0
.idea/gradle.xml View File

@@ -1,5 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project version="4">
3
+  <component name="GradleMigrationSettings" migrationVersion="1" />
3 4
   <component name="GradleSettings">
4 5
     <option name="linkedExternalProjectsSettings">
5 6
       <GradleProjectSettings>

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

+ 1
- 1
src/main/java/CuisineHTTPServer.java View File

@@ -80,7 +80,7 @@ public class CuisineHTTPServer extends Thread
80 80
 
81 81
         protected CuisineWebSocketMessageHandler( CuisineTCPServer iTCPServer)
82 82
         {
83
-        fTCPServer = iTCPServer;
83
+            fTCPServer = iTCPServer;
84 84
         }
85 85
 
86 86
         protected void onFullTextMessage(WebSocketChannel iChannel, BufferedTextMessage iTextMessage)

+ 71
- 4
src/main/java/CuisineTCPServer.java View File

@@ -1,16 +1,83 @@
1
-public class CuisineTCPServer {
1
+import java.io.*;
2
+import java.net.ServerSocket;
3
+import java.net.Socket;
2 4
 
3
-    private float fCurrentTemperature;
5
+public class CuisineTCPServer extends Thread{
4 6
 
7
+    private float fCurrentTemperature;
8
+    private ServerSocket fServerSocket;
5 9
 
6 10
     public CuisineTCPServer()
7 11
     {
8
-        fCurrentTemperature = 0.0f;
12
+        try {
13
+            fServerSocket = new ServerSocket(6868);
14
+            start();
15
+
16
+            fCurrentTemperature = 0.0f;
17
+        }
18
+        catch(Exception ve)
19
+        {
20
+            ve.printStackTrace();
21
+        }
9 22
     }
10 23
 
11 24
     public float getCurrentTemperature()
12 25
     {
13
-        fCurrentTemperature += 1.1f ;
14 26
         return fCurrentTemperature;
15 27
     }
28
+
29
+    public void run()
30
+    {
31
+        while(true)
32
+        {
33
+            try{
34
+                Socket vConnection = fServerSocket.accept();
35
+                System.out.println("Accepted new connection");
36
+                new CuisineTalker(vConnection).start();
37
+            }
38
+            catch(Exception ve)
39
+            {
40
+                ve.printStackTrace();
41
+            }
42
+
43
+        }
44
+    }
45
+
46
+    private class CuisineTalker
47
+        extends Thread
48
+    {
49
+        private Socket fSocket;
50
+
51
+        public CuisineTalker(Socket iSocket)
52
+        {
53
+            fSocket = iSocket;
54
+        }
55
+
56
+        public void run()
57
+        {
58
+            while(true)
59
+            {
60
+                try
61
+                {
62
+                    InputStream vInputStream    = fSocket.getInputStream();
63
+                    BufferedReader vBuffReader  = new BufferedReader(new InputStreamReader(vInputStream));
64
+
65
+                    try{
66
+                        fCurrentTemperature = Float.parseFloat(vBuffReader.readLine());
67
+                    }
68
+                    catch(Exception ve)
69
+                    {
70
+                        ve.printStackTrace();
71
+                    }
72
+
73
+                }
74
+                catch(Exception ve)
75
+                {
76
+                    ve.printStackTrace();
77
+                }
78
+            }
79
+        }
80
+
81
+    }
82
+
16 83
 }

Loading…
Cancel
Save