Linux下实现客户端和服务器端的通信

首先,可以将代码复制下来放到U盘里,然后挂载到Linux上

挂载步骤

找到设备->USB->你U盘的名字

 

挂载成功

访问U盘把代码拷贝到home文件夹下,就可以直接进行编译。

client.c 

    #include <stdio.h> #include <unistd.h>#include <strings.h>#include<string.h>#include <sys/types.h> #include <sys/socket.h> #include <stdlib.h>#include <netinet/in.h> #include <netdb.h>        #define PORT 1234   #define MAXDATASIZE 100  char receiveM[100];char sendM[100]; int main(int argc, char *argv[]) { int fd, numbytes;    struct hostent *he;       struct sockaddr_in server;  //检查用户输入,如果用户输入不正确,提示用户正确的输入方法if (argc !=2) {         printf("Usage: %s <IP Address>\n",argv[0]); exit(1); } // 通过函数 gethostbyname()获得字符串形式的ip地址,并赋给heif ((he=gethostbyname(argv[1]))==NULL){  printf("gethostbyname() error\n"); exit(1); } // 产生套接字fdif ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ printf("socket() error\n"); exit(1); } bzero(&server,sizeof(server));server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr);  if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){  printf("connect() error\n"); exit(1); } // 向服务器发送数据
printf("send message to server:");fgets(sendM,100,stdin);int send_le;send_le=strlen(sendM);sendM[send_le-1]='\0';send(fd,sendM,strlen(sendM),0); // 从服务器接收数据if ((numbytes=recv(fd,receiveM,MAXDATASIZE,0)) == -1){ printf("recv() error\n"); exit(1); } printf("receive message from server:%s\n",receiveM);close(fd); }

server.c

    #include <stdio.h>   #include <string.h>         #include <strings.h>  #include <stdlib.h>       #include <unistd.h>          #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>#include<pthread.h>#define PORT 1234 #define BACKLOG 1 void *start_routine( void *ptr) 
{int fd = *(int *)ptr;char buf[100];int numbytes;int i,c=0;printf("this is a new thread,you got connected\n");printf("fd=%d\n",fd);if ((numbytes=recv(fd,buf,100,0)) == -1){ printf("recv() error\n"); exit(1); } char str[numbytes];char buffer[numbytes];//将收到的字符串反转for(c=0;c<(numbytes-1);c++){buffer[c]=buf[c];}printf("receive message:%s\n",buf);printf("numbytes=%d\n",numbytes);for(i=0;i<numbytes;i++){str[i]=buf[numbytes-1-i];}printf("server will send:%s\n",str);numbytes=send(fd,str,sizeof(str),0); printf("send numbytes=%d\n",numbytes);close(fd);
}int  main()  
{ int listenfd, connectfd;    struct sockaddr_in server; struct sockaddr_in client;      int sin_size; sin_size=sizeof(struct sockaddr_in); pthread_t thread; //定义一个线程号if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {   perror("Creating socket failed.");exit(1);}int opt = SO_REUSEADDR;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));bzero(&server,sizeof(server));  server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr.s_addr = htonl (INADDR_ANY); // 绑定if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) { perror("Bind error.");exit(1); }   // 监听 if(listen(listenfd,BACKLOG) == -1){  /* calls listen() */ perror("listen() error\n"); exit(1); } while(1){// accept if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {perror("accept() error\n"); exit(1); }printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); pthread_create(&thread,NULL,start_routine,(void *)&connectfd);}close(listenfd);}

代码从U盘复制到home后,编译执行

打开三个中端

 

可以发现用户端和服务器端连接成功,即用户端发送的信息,服务器端可以接收的到。

 

转载于:https://www.cnblogs.com/weixinyu98/p/11057217.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/362288.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gvim 安装 taglist

gvim 安装 taglist windows XP下&#xff0c;gvim,安装taglisttaglist 是在vim 下可以像 vc当中的可以列出类&#xff0c;函数 的插件。一&#xff0c;下载ctags,将其中的 ctags.exe 复制到gvim.exe 所在的目录&#xff0c;我的是 C:\Program Files\Vim\vim73如果不复制的话&am…

js笔记(八)ES6

大标题补充描述一、 ES6 中新增的声明方式&#xff1a;let、constvar、let、const之间的区别二、 ES6 字符串扩展1. 子串的识别&#xff1a;includes()、startsWith()、endsWith()&#xff1b;2. 重复字符串&#xff1a;repeat()&#xff1b;3. 字符串补全&#xff1a;padStart…

卡夫卡编年史队列基准

总览 最近&#xff0c;我被要求比较《卡夫卡》和《编年史》的性能。 没有两个产品是完全一样的&#xff0c;要进行公平的比较并不容易。 我们可以尝试运行类似的测试&#xff0c;看看会得到什么结果。 该测试基于Apache Kafka性能结果 。 测试使用了什么&#xff1f; 卡夫卡测…

webpack css打包为一个css

1、安装 npm install extract-text-webpack-plugin --save-dev 2、项目目录&#xff1a; index文件夹下的index.css&#xff1a; body{background-color: #ccc;}.flex-div{display: flex;} index文件夹下的index2.css&#xff1a; p{text-indent: 2em;} index文件夹下的index-l…

javascript深入理解js闭包

闭包&#xff08;closure&#xff09;是Javascript语言的一个难点&#xff0c;也是它的特色&#xff0c;很多高级应用都要依靠闭包实现。 一、变量的作用域 要理解闭包&#xff0c;首先必须理解Javascript特殊的变量作用域。 变量的作用域无非就是两种&#xff1a;全局变量和局…

Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)

因为不会打公式&#xff0c;随意就先将就一下&#xff1f; #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<vector> using namespace std; typedef long long LL; const int N55; const int MOD1e97; int…

新的开始 和一些总结

接触编程不久&#xff0c;2年而已&#xff0c;也不精通&#xff0c;看得比较泛。java&#xff0c;C&#xff0c;C#都有所涉猎&#xff0c;但是仅仅停留于可以编码的阶段&#xff0c;让我就某个问题给出专业的解释&#xff0c;是断无可能的。现在准备考研了&#xff0c;很长一段…

jQuery实现页面关键词高亮

示例代码&#xff0c;关键位置做了注释&#xff0c;请查看代码&#xff1a; <html><head><title>jQuery实现页面关键词高亮</title><style type"text/css">* {margin: 0;padding: 0;}p {padding: 10px;margin-bottom: 20px;}.highligh…

简而言之,JUnit:测试隔离

作为顾问&#xff0c;我仍然经常遇到程序员&#xff0c;他们对JUnit及其正确用法的理解最多。 这使我有了编写多部分教程的想法&#xff0c;从我的角度解释了要点。 尽管存在一些有关使用该工具进行测试的好书和文章&#xff0c;但是也许可以通过本动手实践的方法来使一两个额…

Apache Camel 2.14中的更多指标

Apache Camel 2.14将于本月晚些时候发布。 由于正在解决某些Apache基础结构问题&#xff0c;因此存在一些问题。 这篇博客文章讨论的是我们添加到此版本中的新功能之一。 感谢Lauri Kimmel捐赠了骆驼指标组件&#xff0c;我们将其与出色的Codehale指标库集成在一起。 因此&am…

ASP.NET进阶(8):HttpModule和HttpApplication

前面三节讲了控件的构造、呈现和数据绑定&#xff0c;我想该差不多了。本想讲一个自定义控件来终结控件部分&#xff0c;但是我个人不太喜欢控件这些东西&#xff0c;所以也就懒的写相关的内容&#xff0c;抱歉了。虽然我不喜欢使用控件&#xff0c;但我还是喜欢整个WebForm的设…

移动端网页宽度值(未加meta viewport标签)

移动端网页宽度值&#xff08;未加meta viewport标签&#xff09;: iphone:980px Galaxy&#xff08;盖乐世&#xff09;&#xff1a;980px Nexus&#xff1a;980px blackberry&#xff08;黑莓&#xff09;&#xff1a;980px LG&#xff1a;980px Nokia&#xff1a;980p…

简而言之:JRunner

关于JUnit测试要点的多篇教程的第四章介绍了该工具可交换测试运行器体系结构的目的&#xff0c;并介绍了一些可用的实现。 正在进行的示例通过编写参数化测试的不同可能性扩大了主题。 由于我已经发布了JUnit Rules的介绍&#xff0c;因此我决定跳过关于该主题的已宣布部分。 …

cmake how to create vs file filters

cmake how to create vs file filters 用cmakelists构建出来的工程&#xff0c;没有文件filters&#xff0c;可采用如下方法解决 set(SOURCE_LIST"lotteryTicket.cpp""stdafx.cpp""stdafx.h""test/main.cpp" )add_executable(lotteryT…

Cannot retrieve mapping for action

想必用过Struts的朋友都遇到过这个异常吧&#xff01;没遇到的也可能&#xff0c;只能说你很强或运气不错。 我遇到该异常的解释是我不强&#xff0c;用Struts不是很多&#xff0c;或者说根本不熟练&#xff0c;对一些知识了解得并不深&#xff0c;仅仅皮毛而已&#xff0c;所以…

休眠字节码增强

介绍 既然您了解了Hibernate脏检查的基础知识 &#xff0c;我们就可以研究增强的脏检查机制。 虽然默认的图遍历算法对于大多数用例可能已足够&#xff0c;但有时您需要优化的脏检查算法&#xff0c;并且检测方法比构建自己的自定义策略更方便。 使用Ant休眠工具 传统上&#…

Hibernate核心接口

一、Configuration类&#xff1a;1、 作用&#xff1a;&#xff08;1&#xff09;管理hibernate配置信息&#xff08;2&#xff09;读取hibernate.cfg.xml文件&#xff08;3&#xff09;加载hibernate的驱动&#xff0c;例如&#xff1a;url,用户名&#xff08;4&#xff09;管…

CSS实现垂直居中的方法

CSS实现垂直居中的方法 1、relative absolute定位&#xff1a; (1)css html代码 1 <!doctype html>2 <html lang"en">3 4 <head>5 <meta charset"UTF-8" />6 <title>Document</title>7 …

Neo4j:收集多个值

在Neo4j的密码查询语言中&#xff0c;我最喜欢的功能之一是COLLECT&#xff0c;它使我们能够将项目分组到一个数组中以备后用。 但是&#xff0c;我注意到人们有时很难弄清楚如何使用COLLECT收集多个项目&#xff0c;并且很难找到一种方法。 考虑以下数据集&#xff1a; cre…

spring 概念理解(资料)

一、Spring的IoC(Inversion of Control)。这是Spring中得有特点的一部份。IoC又被翻译成“控制反转”&#xff0c;也不知道是谁翻译得这么别扭&#xff0c;感觉很深奥的词。其实&#xff0c;原理很简单&#xff0c;用一句通俗的话来说&#xff1a;就是用XML来定义生成的对象。I…