-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (137 loc) · 3.14 KB
/
main.go
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
151
package main
import (
"net"
"log"
"os"
"fmt"
"strings"
)
type Server struct {
address string
port string
}
func NewServer(addr,p string) *Server{
log.Printf("Creating new server with address %v and port %v",addr,p)
server:=&Server{
address:addr,
port:p,
}
return server
}
func (s *Server) Listen(){
log.Println("Listening to server: ",s.address+":"+s.port)
listener,err:=net.Listen("tcp", s.address+":"+s.port)
if err!=nil{
log.Fatal("Error listening: ",err)
os.Exit(1)
}
defer listener.Close()
for{
conn,err:=listener.Accept()
if err!=nil{
log.Println("Error accepting connections: ",err)
panic(err)
}
client:=&Client{
conn:conn,
}
go client.handleConnection()
}
}
type Client struct{
conn net.Conn
}
func (c *Client) Addr() string{
return c.conn.RemoteAddr().String()
}
func (c *Client) Close(){
c.conn.Close()
log.Printf("Connection with %v closed",c.Addr())
}
func (c *Client) ChooseMode()string{
buf := make([]byte, 1024)
n, er := c.conn.Read(buf)
if er != nil{
fmt.Println("Error reading from client mode type: ", er)
os.Exit(1)
}
return string(buf[:n])
}
func (c *Client) SendString(message string){
c.conn.Write([]byte(message))
}
//GetData read data from connection
func GetData(conn net.Conn)string{
data:=make([]byte,1024)
n,err:=conn.Read(data)
if err!=nil{
log.Fatal("Error reading data from client: ",err)
}
return string(data[:n])
}
func (c *Client) handleConnection(){
log.Println("Listening for connection on: ", c.Addr())
c.SendString("Hello " + c.Addr() + "\n")
command:=c.ChooseMode()
if command=="file"{
log.Println("Client want to upload file")
Uploadfile(c.conn)
log.Println("Successfully uploaded file")
}else if command=="r"{
log.Println("Client chose routine mode")
for {
input:=GetData(c.conn)
_, e := c.conn.Write([]byte(input))
if e != nil {
fmt.Println("Error sending to client: ", e)
}
log.Println("Sending to client: ", string(input))
input = input[:0]
}
} else{
log.Println("Indicated incorrect flag. Flag was: ",command)
}
c.Close()
}
//CreateStorage create storage for files which will be uploaded to server
func CreateStorage() (path string){
userHome:=os.Getenv("HOME")
path = userHome+"/TCPServerStorage/"
err:=os.MkdirAll(path,os.ModePerm)
if err!=nil{
fmt.Println("Error creating file storage: ",err)
}
return path
}
func Uploadfile(conn net.Conn){
log.Println("File uploading started")
defer conn.Close()
filedata:=GetData(conn)
fd:=strings.Split(filedata,"/")
if len(fd)==0{
log.Println("Error filename and data are empty.")
conn.Close()
}
filename:=fd[0]
data:=fd[1]
log.Println("Successfully got data from client")
log.Println("Creating file storage")
fs:=CreateStorage()
log.Println("Succesfully created file storage at: ",fs)
log.Println("Creating file")
file, err := os.Create(fs+filename)
if err != nil {
log.Fatal("Error in creation file: ",err)
}
log.Println("Successfully created file ",filename)
defer file.Close()
_,err=file.WriteString(data)
if err!=nil{
log.Fatal("Error in writing data to file: ",err)
}
log.Println("Uploaded file: ",filename)
}
func main(){
s:=NewServer("","8080")
s.Listen()
}