Last active
February 12, 2016 18:43
-
-
Save hoenirvili/d08a9edfe9eb8bd35ce1 to your computer and use it in GitHub Desktop.
TCP File client server in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"net" | |
"os" | |
) | |
const ( | |
portAddr = 9632 | |
proto = "tcp" | |
buffSize = 1024 | |
) | |
var ( | |
ipAddr net.IP = net.ParseIP("127.0.0.1") | |
bufferRead = make([]byte, buffSize) | |
) | |
func main() { | |
// ptr to conf | |
confTcp := &net.TCPAddr{ | |
IP: ipAddr, | |
Port: portAddr, | |
} | |
sock, err := net.DialTCP(proto, nil, confTcp) | |
if err != nil { | |
fmt.Fprintf(os.Stdout, "[#] \tCant create tcp socket dialer\n") | |
fmt.Fprintf(os.Stderr, "[#] \t %s\n", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Fprintf(os.Stdout, "[#] \t Server said \n") | |
for { | |
n, err := sock.Read(bufferRead) | |
if err != nil && err != io.EOF { | |
fmt.Fprintf(os.Stdout, "\n[#] \tCant read from tcp socket\n") | |
fmt.Fprintf(os.Stderr, "[#], \t %s\n", err.Error()) | |
break | |
} | |
if n > 0 { | |
fmt.Fprintf(os.Stdout, "%s", string(bufferRead)) | |
} else { | |
break | |
} | |
} | |
defer func() { | |
if err := sock.Close(); err != nil { | |
panic(err) | |
} | |
fmt.Fprintf(os.Stdout, "\n[#] \t Client close connection\n") | |
}() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lorem ipsum dolor sit amet, duis aliquet, vulputate consequat eros nunc, sapien mauris nullam, sit risus fugiat eleifend nunc. Turpis justo luctus tristique ridiculus, gravida morbi tellus sollicitudin in id. Consequat praesent nibh donec, erat ante nec eget, mollis lectus adipiscing, erat aut luctus cubilia ultrices, consequat amet maecenas quis. Morbi aliquam tempus eget vulputate wisi, dolor habitant est, aliquam magna, nunc porttitor vestibulum, dapibus libero vitae eu suspendisse lectus. Ipsum vel dignissim, lacus mollis nisl et tincidunt eros, at bibendum in tincidunt, augue aptent id ridiculus ante taciti sed, maecenas massa sed. A est. Quam aliquet, mauris at turpis semper augue, vitae cras pede a consequat congue lobortis, tristique nullam malesuada augue amet libero. Sapien in eget, at dui purus, vestibulum eget vel. In morbi pharetra nunc metus. Etiam nec erat nibh nunc, sint amet phasellus metus, sit suscipit mollis similique. Ipsum cras id, etiam morbi sed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"net" | |
"os" | |
) | |
const ( | |
portAddr = 9632 | |
proto = "tcp" | |
buffSize = 1024 | |
) | |
var ( | |
ipAddr net.IP = net.ParseIP("127.0.0.1") | |
buffer = make([]byte, buffSize) | |
) | |
func main() { | |
// define our tcpAddr conf | |
confTcp := &net.TCPAddr{ | |
IP: ipAddr, | |
Port: portAddr, | |
} | |
// create listen tcp sock | |
sock, err := net.ListenTCP(proto, confTcp) | |
if err != nil { | |
fmt.Fprintf(os.Stdout, "[#] \tCant create tcp socket listener\n") | |
fmt.Fprintf(os.Stderr, "[#] \t %s\n", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Fprintf(os.Stdout, "[#] \tRunning the %s server %s : %d\n", proto, ipAddr.String(), portAddr) | |
for { | |
// forever client | |
client, err := sock.Accept() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "[#] \t %s\n", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Fprintf(os.Stdout, "[#] \tClient connected\n") | |
manageFile(client, buffer, "file.txt") | |
fmt.Fprintf(os.Stdout, "[#] \tClosing the client proto %s on conn %s\n", client.LocalAddr().Network(), client.LocalAddr().String()) | |
//close connection | |
client.Close() | |
} | |
} | |
func manageFile(sock net.Conn, buffer []byte, filePath string) { | |
fp, err := os.Open(filePath) | |
if err != nil { | |
panic(err) | |
} | |
for { | |
n, err := fp.Read(buffer) | |
if err != nil && err != io.EOF { | |
panic(err) | |
} | |
if n == 0 || err == io.EOF { | |
break | |
} else { | |
m, err := sock.Write(buffer) | |
if err != nil && m <= 0 { | |
fmt.Fprintf(os.Stderr, "[#] \t Server can't send msg\n") | |
os.Exit(1) | |
} | |
} | |
} | |
defer func() { | |
if err := fp.Close(); err != nil { | |
panic(err) | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment