南昌大学实验报告
姓名:王梦佳
学号:6103115097
邮箱地址:[email protected]
专业班级:计科153班
实验日期:2018.5.27
课程名称:LINUX程序设计
实验项目名称
Lab 6 : Socket It Out
实验目的
• Understanding the mechanism of socket
• Trying to learn some C
• Understanding the process of network programming
实验步骤
Task 1 Socket it
• Program a C/S communication in one host
• Echo the input from client at the server side
• Using socket interface to implement such process
• Make a real pair using Linux C
Task 2 Easier Job on the Way
• Repeat the task in the last slide, using Golang
实验数据或结果
task1客户端:
include
include
include
include
include
include
include
include
define MAXLINE 80
define SERV_PORT 8000
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd, n;
char *str;
if (argc != 2) {
fputs("usage: ./client message\n", stderr);
exit(1);
}
str = argv[1];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
write(sockfd, str, strlen(str));
n = read(sockfd, buf, MAXLINE);
printf("Response from server:\n");
write(STDOUT_FILENO, buf, n);
close(sockfd);
return 0;
}
服务器端:
include
include
include
include
include
include
include
include
define MAXLINE 80
define SERV_PORT 8000
int main(void)
{
struct sockaddr_in servaddr, cliaddr;
socklen_t cliaddr_len;
int listenfd, connfd;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
int i, n;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, 20);
printf("Accepting connections ...\n");
while (1) {
cliaddr_len = sizeof(cliaddr);
connfd = accept(listenfd,
(struct sockaddr *)&cliaddr, &cliaddr_len);
n = read(connfd, buf, MAXLINE);
printf("received from %s at PORT %d\n",inet_ntop(AF_INET,
&cliaddr.sin_addr,
str, sizeof(str)),ntohs(cliaddr.sin_port));
for (i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
write(connfd, buf, n);
close(connfd);
}
}
实验截图:
task2客户端:
package main
import (
"fmt"
"net"
)
const (
addr = "127.0.0.1:3333"
)
func main() {
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Println("连接服务端失败:", err.Error())
return
}
fmt.Println("已连接服务器")
defer conn.Close()
Client(conn)
}
func Client(conn net.Conn) {
sms := make([]byte, 1024)
for {
fmt.Print("请输入要发送的消息:")
_, err := fmt.Scan(&sms)
if err != nil {
fmt.Println("数据输入异常:", err.Error())
}
conn.Write(sms)
buf := make([]byte, 1024)
c, err := conn.Read(buf)
if err != nil {
fmt.Println("读取服务器数据异常:", err.Error())
}
fmt.Println(string(buf[0:c]))
}
}
服务器端:
package main
import (
"fmt"
"net"
)
const (
//绑定IP地址
ip = "127.0.0.1"
//绑定端口号
port = 3333
)
func main() {
listen, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP(ip), port, ""})
if err != nil {
fmt.Println("监听端口失败:", err.Error())
return
}
fmt.Println("已初始化连接,等待客户端连接...")
Server(listen)
}
func Server(listen *net.TCPListener) {
for {
conn, err := listen.AcceptTCP()
if err != nil {
fmt.Println("接受客户端连接异常:", err.Error())
continue
}
fmt.Println("客户端连接来自:", conn.RemoteAddr().String())
defer conn.Close()
go func() {
data := make([]byte, 1024)
for {
i, err := conn.Read(data)
fmt.Println("客户端", conn.RemoteAddr().String(), "发来数据:", string(data[0:i]))
if err != nil {
fmt.Println("读取客户端数据错误:", err.Error())
break
}
if string(data[:i]) == "exit" {
conn.Write([]byte{'e', 'x', 'i', 't'})
conn.Close()
}
conn.Write(data[0:i])
}
}()
}
}
实验截图:
参考资料
https://www.jianshu.com/
书上的9-2例题