diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index a7618ceb..cf296a24 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -313,6 +313,10 @@ func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) { return } +func Send(s int, buf []byte, flags int) (err error) { + return sendto(s, buf, flags, nil, 0) +} + func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) { ptr, n, err := to.sockaddr() if err != nil { diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go index 91b02b31..2f672832 100644 --- a/unix/syscall_unix_test.go +++ b/unix/syscall_unix_test.go @@ -8,6 +8,7 @@ package unix_test import ( + "bytes" "flag" "fmt" "io/ioutil" @@ -890,6 +891,44 @@ func TestUtimesNanoAt(t *testing.T) { } } +func TestSend(t *testing.T) { + ec := make(chan error, 2) + ts := []byte("HELLO GOPHER") + + fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0) + if err != nil { + t.Fatalf("Socketpair: %v", err) + } + defer unix.Close(fds[0]) + defer unix.Close(fds[1]) + + go func() { + data := make([]byte, len(ts)) + + _, _, err := unix.Recvfrom(fds[1], data, 0) + if err != nil { + ec <- err + } + if !bytes.Equal(ts, data) { + ec <- fmt.Errorf("data sent != data received. Received %q", data) + } + ec <- nil + }() + err = unix.Send(fds[0], ts, 0) + if err != nil { + ec <- err + } + + select { + case err = <-ec: + if err != nil { + t.Fatalf("Send: %v", err) + } + case <-time.After(2 * time.Second): + t.Fatal("Send: nothing received after 2 seconds") + } +} + // mktmpfifo creates a temporary FIFO and provides a cleanup function. func mktmpfifo(t *testing.T) (*os.File, func()) { err := unix.Mkfifo("fifo", 0666)