Last active
December 17, 2021 07:58
-
-
Save growvv/b880a118b1ab8609da9dc0231cb5208d to your computer and use it in GitHub Desktop.
基于uthread库的协程echo server,与go协程写法基本一致
This file contains hidden or 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" | |
"net" | |
) | |
func main() { | |
fmt.Println("Starting the server ...") | |
// 创建 listener | |
listener, err := net.Listen("tcp", "localhost:50000") | |
if err != nil { | |
fmt.Println("Error listening", err.Error()) | |
return //终止程序 | |
} | |
// 监听并接受来自客户端的连接 | |
for { | |
conn, err := listener.Accept() | |
fmt.Printf("Accepted connection from %s\n", conn.RemoteAddr()) | |
if err != nil { | |
fmt.Println("Error accepting", err.Error()) | |
return // 终止程序 | |
} | |
go doServerStuff(conn) | |
} | |
} | |
func doServerStuff(conn net.Conn) { | |
for { | |
buf := make([]byte, 512) | |
len, err := conn.Read(buf) | |
if err != nil { | |
fmt.Println("Error reading", err.Error()) | |
fmt.Printf("Closing connection with %s\n", conn.RemoteAddr()) | |
conn.Close() | |
return //终止程序 | |
} | |
fmt.Printf("Received data: %v\n", string(buf[:len])) | |
conn.Write(buf[:len]) | |
} | |
} |
This file contains hidden or 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
#include<stdio.h> | |
#include<errno.h> | |
#include<err.h> | |
#include<string.h> | |
#include<sys/socket.h> | |
#include<arpa/inet.h> | |
#include "uthread.h" | |
/* Port to listen on. */ | |
#define SERVER_PORT 5555 | |
/* Connection backlog (# of backlogged connections to accept). */ | |
#define CONNECTION_BACKLOG 8 | |
void* doServerStuff(void*); | |
void* hello(void*); | |
int main() { | |
enable_hook(); | |
int listenfd, clientfd; | |
struct sockaddr_in listen_addr; | |
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
err(1, "listen failed"); | |
} | |
memset(&listen_addr, 0, sizeof(listen_addr)); | |
listen_addr.sin_family = AF_INET; | |
listen_addr.sin_addr.s_addr = INADDR_ANY; | |
listen_addr.sin_port = htons(SERVER_PORT); | |
if (bind(listenfd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { | |
err(1, "bind failed"); | |
} | |
if (listen(listenfd, CONNECTION_BACKLOG) < 0) { | |
err(1, "listen failed"); | |
} | |
pthread_t init_p; | |
pthread_create(&init_p, NULL, hello, NULL); | |
while(1) { | |
if ( (clientfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1) { | |
printf("accept socket error:%s(errno:%d)\n", strerror(errno), errno); | |
continue; | |
} | |
printf("accept a new client:%d\n", clientfd); | |
pthread_t p; | |
pthread_create(&p,NULL, doServerStuff, &clientfd); | |
} | |
main_end(); | |
} | |
void* doServerStuff(void *arg) { | |
int clientfd = *(int *)arg; | |
char buf[1024]; | |
int n; | |
while(1) { | |
printf("clientfd:%d\n", clientfd); | |
n = read(clientfd, buf, sizeof(buf)); | |
if (n == 0) { | |
printf("client close\n"); | |
break; | |
} | |
write(clientfd, buf, n); | |
} | |
} | |
void* hello(void* arg) { | |
printf("init\n"); | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment