initializ project
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.log
|
65
client.go
Normal file
65
client.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
host = flag.String("host", "localhost", "Server host")
|
||||
port = flag.String("port", "26666", "Server port")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Connect to the server
|
||||
conn, err := net.Dial("tcp", *host+":"+*port)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to connect to server: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
fmt.Printf("Connected to server %s:%s\n", *host, *port)
|
||||
fmt.Println("Type messages to send to the server (type 'exit' to quit):")
|
||||
|
||||
// Create a scanner to read from standard input
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
// Create a goroutine to read messages from the server
|
||||
go func() {
|
||||
serverScanner := bufio.NewScanner(conn)
|
||||
for serverScanner.Scan() {
|
||||
fmt.Printf("Server: %s\n", serverScanner.Text())
|
||||
}
|
||||
}()
|
||||
|
||||
// Read from standard input and send to server
|
||||
for {
|
||||
if !scanner.Scan() {
|
||||
break
|
||||
}
|
||||
|
||||
message := scanner.Text()
|
||||
if strings.TrimSpace(message) == "exit" {
|
||||
fmt.Println("Exiting...")
|
||||
break
|
||||
}
|
||||
|
||||
// Send message to server
|
||||
_, err := fmt.Fprintln(conn, message)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to send message: %v\n", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Printf("Error reading input: %v\n", err)
|
||||
}
|
||||
}
|
89
server.go
Normal file
89
server.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.String("port", "26666", "Port to listen on")
|
||||
logFile = flag.String("logfile", "server.log", "Log file path")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Open log file
|
||||
file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to open log file:", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Set log output to both file and console
|
||||
log.SetOutput(file)
|
||||
|
||||
// Start listening on specified port
|
||||
listener, err := net.Listen("tcp", ":"+*port)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to start server:", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
|
||||
fmt.Printf("Server listening on port %s\n", *port)
|
||||
log.Printf("Server listening on port %s\n", *port)
|
||||
|
||||
for {
|
||||
// Accept incoming connections
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Printf("Failed to accept connection: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle each connection in a separate goroutine
|
||||
go handleConnection(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func handleConnection(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
// Get connection info
|
||||
remoteAddr := conn.RemoteAddr().(*net.TCPAddr)
|
||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
// Print and log connection info
|
||||
connInfo := fmt.Sprintf("New connection from IP: %s, Port: %d, Timestamp: %s",
|
||||
remoteAddr.IP, remoteAddr.Port, timestamp)
|
||||
fmt.Println(connInfo)
|
||||
log.Println(connInfo)
|
||||
|
||||
// Create a scanner to read messages from the client
|
||||
scanner := bufio.NewScanner(conn)
|
||||
for scanner.Scan() {
|
||||
message := scanner.Text()
|
||||
msgTimestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
// Print and log the received message with timestamp
|
||||
logEntry := fmt.Sprintf("[%s] Received: %s", msgTimestamp, message)
|
||||
fmt.Println(logEntry)
|
||||
log.Println(logEntry)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
errorMsg := fmt.Sprintf("Error reading from connection: %v", err)
|
||||
fmt.Println(errorMsg)
|
||||
log.Println(errorMsg)
|
||||
}
|
||||
|
||||
// Log when client disconnects
|
||||
disconnectMsg := fmt.Sprintf("Client %s:%d disconnected", remoteAddr.IP, remoteAddr.Port)
|
||||
fmt.Println(disconnectMsg)
|
||||
log.Println(disconnectMsg)
|
||||
}
|
Reference in New Issue
Block a user