首页 > 其他分享 >学习笔记11+代码

学习笔记11+代码

时间:2023-11-25 16:13:12浏览次数:28  
标签:11 addr 代码 笔记 server client printf include socket

一、苏格拉底挑战









二、遇见的问题


三、实践和代码


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX 256
#define SERVER_HOST "localhost"
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 1234
struct sockaddr_in server_addr, client_addr;
int mysock, csock; // socket descriptors
int r, len, n; // help variables
int server_init()
{
printf("================== server init ======================\n");
// create a TCP socket by socket() syscall
printf("1 : create a TCP STREAM socket\n");
mysock = socket(AF_INET, SOCK_STREAM, 0);
if (mysock < 0){
printf("socket call failed\n"); exit(1);
}
printf("2 : fill server_addr with host IP and PORT# info\n");
// initialize the server_addr structure
server_addr.sin_family = AF_INET; // for TCP/IP
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // This HOST IP
server_addr.sin_port = htons(SERVER_PORT); // port number 1234
printf("3 : bind socket to server address\n");
r = bind(mysock,(struct sockaddr*)&server_addr,sizeof(server_addr));
if (r < 0){
printf("bind failed\n"); exit(3);
}
printf(" hostname = %s port = %d\n", SERVER_HOST, SERVER_PORT);
printf("4 : server is listening ....\n");
listen(mysock, 5); // queue length = 5
printf("=================== init done =======================\n");
}
int main()
{
char line[MAX];
server_init();
while(1){ // Try to accept a client request
printf("server: accepting new connection ....\n");
// Try to accept a client connection as descriptor newsock
len = sizeof(client_addr);
csock = accept(mysock, (struct sockaddr *)&client_addr, &len);
if (csock < 0){
printf("server: accept error\n"); exit(1);
}
printf("server: accepted a client connection from\n");
printf("-----------------------------------------------\n");
printf("Clinet: IP=%s port=%d\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
printf("-----------------------------------------------\n");
// Processing loop: client_sock <== data ==> client
while(1){
n = read(csock, line, MAX);
if (n==0){
printf("server: client died, server loops\n");
close(csock);
break;
}
// show the line string
printf("server: read n=%d bytes; line=%s\n", n, line);
// echo line to client
n = write(csock, line, MAX);
printf("server: wrote n=%d bytes; ECHO=%s\n", n, line);
printf("server: ready for next request\n");
}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX 256
#define SERVER_HOST "localhost"
#define SERVER_PORT 1234
struct sockaddr_in server_addr;
int sock, r;
int client_init()
{
printf("======= clinet init ==========\n");
printf("1 : create a TCP socket\n");
sock = socket(AF_INET , SOCK_STREAM, 0);
if (sock<0){
printf("socket call failed\n"); exit(1);
}
printf("2 : fill server_addr with server's IP and PORT#\n");
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // localhost
server_addr.sin_port = htons(SERVER_PORT); // server port number
printf("3 : connecting to server ....\n");
r = connect(sock,(struct sockaddr*)&server_addr, sizeof(server_addr));
if (r < 0){
printf("connect failed\n"); exit(3);
}
printf("4 : connected OK to\n");
printf("-------------------------------------------------------\n");
printf("Server hostname=%s PORT=%d\n", SERVER_HOST, SERVER_PORT);
printf("-------------------------------------------------------\n");
printf("========= init done ==========\n");
}
int main()
{
int n;
char line[MAX], ans[MAX];
client_init();
printf("******** processing loop *********\n");
while (1){
printf("input a line : ");
bzero(line, MAX); // zero out line[ ]
fgets(line, MAX, stdin); // get a line from stdin
line[strlen(line)-1] = 0; // kill \n at end
if (line[0]==0) // exit if NULL line
exit(0);
// Send line to server
n = write(sock, line, MAX );
printf("client: wrote n=%d bytes; line=%s\n", n, line);
// Read a line from sock and show it
n = read(sock, ans, MAX );
printf("client: read n=%d bytes; echo=%s\n", n, ans);
}
}

}
}

标签:11,addr,代码,笔记,server,client,printf,include,socket
From: https://www.cnblogs.com/yuanyi23/p/17855614.html

相关文章

  • P1135 奇怪的电梯
    奇怪的电梯题目背景感谢@yummy提供的一些数据。题目描述呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第\(i\)层楼(\(1\lei\leN\))上有一个数字\(K_i\)(\(0\leK_i\leN\))。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那......
  • 信息安全系统设计与实现 学习笔记11
    《Unix/Linux系统编程》13章学习笔记网络编程简介TCP/IP协议、UDP和TCP协议、服务器—客户机计算、HTTP和Web页面、动态Web页面的PHP和CGI编程TCP/IP协议TCP/IP是互联网的基础,TCP代表传输控制协议,IP代表互联网协议。TCP/IP的组织结构分为几个层级,通常称为TCP/IP堆栈。顶层时......
  • 20231125橡胶
    目前在等待日线有没有一波C3的上涨波段。  具体到下周走势,也会比较跌宕起伏: ......
  • [笔记]加州大学公开课———学会如何学习(全)
    bilibili.com/video/BV1cy4y1p7TC1-5集:专注/发散,讲解了如何使用番茄时钟25分钟,解决拖延症。大脑是通过每天不断学习才能变的更聪明。第7集一天内重复练习20次还是很容易忘记。间隔练习则会长期记录下。第8集睡眠要保持充足,不然你的脑子还是处于高压工作状态,脑子里有毒不清醒......
  • 一个用于多线程共享数据保护测试的简易游戏服务器代码
    #include<iostream>#include<thread>#include<list>#include<mutex>//一个线程负责从客户端读取用户的命令,放入一个队列中;//另一个线程负责从队列中读取命令并解析,假设用一个int变量代表一个命令。classA{public://这里无法模拟从网络接受命令的过程,我们......
  • 2023.11.25-2023.12.31 模拟赛记录
    苟。11.25多校dream给定N,求\(\sum_{i\in\left[1,2^N\right]}\log_2\left(\prod_{j\in[1,i]}\text{lowbit}(j)\right)\)。(N<=1e18)找个规律发现\(\log_2\left(\prod_{j\in[1,i]}\text{lowbit}(j)\right)\)就是\(i-\text{popcount}(i)\)。不难移值域,算贡献。ca......
  • django学习笔记05 跨域
    跨域指的是:浏览器不能执行其他网站的脚本,从一个域名的网页去请求另一个域名的资源时,域名、端口、协议任一不同,都是跨域。跨域是由浏览器的同源策略造成的,是浏览器施加的安全限制。a页面想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,所进行的访问行动都是跨域的。在DJ......
  • 凤凰鸿鹄微代码平台:企业管理与智能制造的降本增效之选
    ​引言在当今高度信息化的时代,企业如何有效利用信息技术提升管理效率、降低运营成本,已成为竞争制胜的关键。在这一背景下,凤凰鸿鹄微代码平台应运而生,它是一款全面而高效的智能制造解决方案,旨在满足企业管理与信息化发展的需求。本文将深入探讨凤凰鸿鹄微代码平台在企业管理与智......
  • webpack原理实战笔记
    我是歌谣想加入前端技术群私信我最近开始webpack原理的书写目录结构核心依赖{"name":"mywebpack","version":"1.0.0","description":"","main":"index.js","scripts":{"test"......
  • 前端学习笔记202307学习笔记第六十七天-前端面试-switch方法
      ......