Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- // This is a parot server
- // January 2018, JCA, IUT, University of Nnantes
- // VERSION QUI RENVOIE UN NOMBRE EN FONCTION DE CE QU'IL Y A DANS LA MAP
- import (
- "net" // for net/socket resources
- "os" // for OS resources
- "fmt" // general formatting
- //"strconv" // Convert str to int or int to
- // "bufio" // intput output package
- )
- //--------------------------------------------
- func interactWithClient(commSock net.Conn) {
- myMap := map[string]string{
- "1":"Salut",
- "2":"Comment va ?",
- "3":"Bien et toi ?",
- "4":"Travail terminé !",
- "42":"BIEN JOUE !",
- }
- // send a Agreement msg to the client
- writelen, err := commSock.Write([]byte("Serv ACK! > Ready to receive\n"))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
- } else {
- fmt.Printf("Wrote %d bytes to socket\n", writelen)
- }
- for ;; { // Server loop to interact with the client
- buf := make([]byte, 256) // for buffering // but redecl :-(
- bufLen, err := commSock.Read(buf) // try a read buffer from the Sock
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error when reading from socket: %s\n", err)
- return
- }
- if bufLen == 0 {
- fmt.Printf("Connection closed by remote host\n")
- return
- }
- fmt.Printf("Length of read buffer = %d\n",bufLen)
- buf = buf[:bufLen-2] // this is to ignore the 2 '\n', counting from 0
- fmt.Printf("Client at %s says %s \n", commSock.RemoteAddr(), buf)
- // prepare the response
- resp := ""
- num := string(buf) // Cast the buff to string
- if myMap[num] == "" {
- resp = "ANS> No value matches"+"\n"
- } else {
- resp = "ANS>"+myMap[num]+"\n"
- }
- // send a 'response' to the client via the same connScock
- writtenLen, err := commSock.Write([]byte(resp))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
- } else {
- fmt.Printf("Wrote %d bytes to socket\n", writtenLen)
- }
- }//for
- }
- func listenForClients(ip string, port string) {
- sockListener, err := net.Listen("tcp", ip + ":" + port)
- if err != nil {
- fmt.Fprintf(os.Stderr, "Could not listen on socket: %s\n", err)
- return
- }
- for ;;{ // Allows to accept many clients
- // try to accept clients on the connection sockect => communic socket
- commSock, err := sockListener.Accept() // wait until a Dial from a client
- if err != nil {
- fmt.Fprintf(os.Stderr, "Could not accept connection on socket: %s\n", err)
- return
- }
- resp := "BONJOUR, Vous êtes sur le serveur " + ip + ":" + port + "\n"
- _, _ = commSock.Write([]byte(resp))
- if port == "2056"{
- interactWithClient(commSock) // interact wih a client in case of a 'Dial'. Launch and continue the for loop
- } else {
- go interactWithClient(commSock) // interact wih a client in case of a 'Dial'. Launch and continue the for loop
- }
- }
- }
- //------------------------------
- func main() {
- // attempt to create a connection sock
- // listening at a port of a machine : localhost:port
- go listenForClients("172.28.11.42","2056")
- listenForClients("172.28.11.42","2057")
- }
- //-----------------------------
- // to run the server after compilation, from an xterm : ./sockServer_1
- //
- // to interact from client side : telnet localhost 2056
- //----------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement