fix: missing lab7

This commit is contained in:
KirisameVanilla 2025-09-11 13:01:55 +08:00
parent 8ecc438125
commit f829b0c9c5
No known key found for this signature in database
GPG Key ID: 7FC750F817277AC5
4 changed files with 9 additions and 136 deletions

View File

@ -1,69 +0,0 @@
/* Source: Peterson & Davie (2007), Computer Networks, a Systems Approach,
* 4th ed., Morgan Kaufmann, p. 34-35.
* Included stdlib.h, string.h, and strings.h so it compiles on Linux.
* Changed port from 5432 (postgresql) to 7701 (unassigned).
* - JLND Feb 7 2009
*/
#include<unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 7701
#define MAX_LINE 256
int
main(int argc, char * argv[])
{
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
int len;
if (argc==2) {
host = argv[1];
}
else {
fprintf(stderr, "usage: simplex-talk host\n");
exit(1);
}
/* translate host name into peers IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
exit(1);
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
exit(1);
}
/* main loop: get and send lines of text */
while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
}
}

View File

@ -1,9 +1,8 @@
# <center>华东师范大学软件学院实验报告</center> ---
title: Lab7 - Socket Programming
---
| **实验课程:** 计算机网络 | **年级:** 2024 | **实验成绩:** | # 华东师范大学软件学院实验报告
| :-------------------------------- | :--------------------- | :------------------------ |
| **实验名称:** Socket Programming | **姓名:** | **实验日期:** 2025.01.03 |
| **实验编号:** 7 | **学号:** | **实验时间:** 2学时 |
## 一、实验目的 ## 一、实验目的
@ -39,7 +38,7 @@
3. 启动0号客户端,此时 Wireshark 捕获到三次握手包观察到0号客户端采用44514端口 3. 启动0号客户端,此时 Wireshark 捕获到三次握手包观察到0号客户端采用44514端口
![png1](./lib7-2.png) ![png1](./lib7-2.png)
4. 发送文本测试 4. 发送文本测试
@ -163,7 +162,7 @@
* Changed port from 5432 (postgresql) to 7701 (unassigned). * Changed port from 5432 (postgresql) to 7701 (unassigned).
* - JLND Feb 7 2009 * - JLND Feb 7 2009
*/ */
#include<unistd.h> #include <unistd.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -176,8 +175,7 @@
#define SERVER_PORT 7701 #define SERVER_PORT 7701
#define MAX_LINE 256 #define MAX_LINE 256
int int main(int argc, char * argv[])
main(int argc, char * argv[])
{ {
FILE *fp; FILE *fp;
struct hostent *hp; struct hostent *hp;
@ -237,7 +235,7 @@ main(int argc, char * argv[])
* Changed port from 5432 (postgresql) to 7701 (unassigned). * Changed port from 5432 (postgresql) to 7701 (unassigned).
* - JLND Feb 7 2009 * - JLND Feb 7 2009
*/ */
#include<unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
@ -250,8 +248,7 @@ main(int argc, char * argv[])
#define MAX_PENDING 5 #define MAX_PENDING 5
#define MAX_LINE 256 #define MAX_LINE 256
int int main()
main()
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
char buf[MAX_LINE]; char buf[MAX_LINE];

View File

@ -1,55 +0,0 @@
/* Source: Peterson & Davie (2007), Computer Networks, a Systems Approach,
* 4th ed., Morgan Kaufmann, p. 35-36.
* Included stdlib.h, string.h, and strings.h so it compiles on Linux.
* Changed port from 5432 (postgresql) to 7701 (unassigned).
* - JLND Feb 7 2009
*/
#include<unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 7701
#define MAX_PENDING 5
#define MAX_LINE 256
int
main()
{
struct sockaddr_in sin;
char buf[MAX_LINE];
int len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
while(1) {
if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while (len = recv(new_s, buf, sizeof(buf), 0))
fputs(buf, stdout);
close(new_s);
}
}