2021.7.16快乐实验

PuTTy

嫖到了高中同学的服务器,用PuTTy去连

2021.7.16快乐实验

  1. 参数举例说明如下:
    • Host Name(or IP address):公网 IP
    • Port:轻量应用服务器的连接端口,必须设置为22
    • Connect type:选择 “SSH”
    • Saved Sessions:填写会话名称,例如 test
      配置 “Host Name” 后,再配置 “Saved Sessions” 并保存,则后续使用时您可直接双击 “Saved Sessions” 下保存的会话名称即可登录服务器
  2. 单击【Open】,进入 “PuTTY” 的运行界面

运行C/C++

安装c/c++环境(gcc)

yum install gcc
yum install gcc-c++ libstdc++-devel

或者

sudo apt-get install gcc

安装完成后可以使用gcc -v来查看编译器版本信息等。

编译C文件:

gcc xxx.c

直接这样会发现出来的是默认的a.out

可以加上-o来定义输出文件

gcc xxx.c -o xxx

使用

./xxx

来执行

要注意需要拥有x(执行)权限才行(我是root所以就默认了)

C++编译时将gcc改成g++即可

新建用户并上传图片

useradd littleblue

su - littblue

打开winscp,使用littleblue账号链接主机

cd /home/littleblue

mkdir setu

在setu里新建Saber文件夹并上传了一个.zip压缩包

发现账号无法进入Saber,利用ls -al查看,发现

drwx------ 2 root       root       4096 Jul 16 14:09 Saber

只有root拥有完全权限,改成root权限

使用chmod 777 -R setu 更改权限

并且使用chown littleblue setu/ 更改拥有者

现在好了,littleblue也可以查看了

drwxrwxrwx  5 littleblue littleblue 4096 Jul 16 14:09 setu

想办法解压

unzip zipped_file.zip

错了,现在图片全解压到文件夹里了,删除一下

shopt -s extglob
rm -rf !(*.java|*.tar.gz)

例如:删除当前目录下除了.tar.gz和.java结尾的其他文件或文件夹

重来

unzip zipped_file.zip -d unzipped_directory

ok

通话

cli.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
 
int exit_flag;

void *threadsend(void *vargp);
void *threadrecv(void *vargp); 

int main(int argc, char **argv)
{ 
  if(argc !=3) 
  {
    printf("start e.g:  ./cli 192.168.5.128 9001\n");
    return -1;
  }		
  //printf("argc %d argv[1]: %s argv[2]: %s\n", argc, argv[1], argv[2]);
  exit_flag = 1;	
  int clientfdp;

  clientfdp = socket(AF_INET,SOCK_STREAM,0);
  
//  int flags = fcntl(clientfdp, F_GETFL, 0);
//  fcntl(clientfdp, F_SETFL, flags | O_NONBLOCK);  
  
  struct sockaddr_in serveraddr;struct hostent *hp;
  bzero((char *)&serveraddr,sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_port = htons(atoi(argv[2]));
  serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
  
  if(connect(clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0)
  {        
    printf("Connect error\n");
    exit(-1);
  }
  
  pthread_t tid1,tid2;
  printf("Connect server success.\n");
  pthread_create(&tid1,NULL,threadsend,&clientfdp);
  pthread_create(&tid2,NULL,threadrecv,&clientfdp);  
  

  if(tid1 !=0) 
  {
     pthread_join(tid1,NULL);
     printf("threadsend  pthread_join completed\n");
  }
  if(tid2 !=0)
  {
     pthread_join(tid2, NULL);
     printf("threadrecv  pthread_join completed\n");
  }
  
  close(clientfdp);
  printf("client close\n");
  return EXIT_SUCCESS;
}

void *threadsend(void * vargp)
{

  int connfd = *((int *)vargp);
  int idata;char temp[100];
  while(exit_flag)
  {
    memset(temp, 0, sizeof(temp));

    fgets(temp,100,stdin);
    if(strlen(temp) > 0)
    {    	
      send(connfd,temp,100,0);
      printf("          message send OK\n");   	
    }
    usleep(500);
    if(memcmp(temp, "exit", 4) == 0)
    {
      exit_flag = 0; break;  		
    }
      
  }
  printf("client send over and exit \n");
  pthread_exit(0);
}

void *threadrecv(void *vargp)
{
  char temp[100];
  int connfd = *((int *)vargp);
  while(exit_flag)
  {
    int idata = 0;
    if(!exit_flag) break;
    idata = recv(connfd,temp,100,0);
    if(idata > 0)
    {
      printf("Message from server :%s\n", temp);
    }
    usleep(500); 
  }  
  
  printf("client threadrecv over and exit \n");
  pthread_exit(0);
}

svr.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>

#define maxsec 20

typedef struct clieninfo
{                 
  int connfdp;            
  signed char padress[20];       
} clieninfoT;

void *createthread(void *vargp);

void *threadsend(void *vargp);
void *threadrecv(void *vargp); 

int exit_flag;

int main(int argc, char **argv)
{ 
	if(argc !=2) 
	{
		printf("start e.g:  ./svr 9001\n");
		return -1;
	}
		
//	printf("argc %d argv[1]: %s \n", argc, argv[1]);
  exit_flag = 1;	
	
  int listenfd = socket(AF_INET, SOCK_STREAM,0);
  if(listenfd < 0)
  {        
    perror("socket err\n");        
    exit(-1);
  }
  
 
  int flags = fcntl(listenfd, F_GETFL, 0);
  fcntl(listenfd, F_SETFL, flags | O_NONBLOCK);
         
  struct hostent *hp;
  struct sockaddr_in serveraddr;
  
  bzero((char *)&serveraddr,sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serveraddr.sin_port = htons(atoi(argv[1]));
  
  if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0)
  {        
    perror("connect error\n");        
    exit(-1);
  }
  
  if(listen(listenfd,10) < 0)
  {        
    perror("listen error");        
    exit(-1);
  } 
  printf("Server started... \n");

  struct sockaddr_in clientaddr;
  int clientlen, i;
  pthread_t sectid[maxsec]; 
  memset(&sectid, 0, maxsec * sizeof(pthread_t));
  i = 0;            
 
 clieninfoT Pclient;
 memset(&Pclient, 0, sizeof(clieninfoT));

  clientlen = sizeof(clientaddr);
  while(exit_flag)
  {
    Pclient.connfdp = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);
    if (Pclient.connfdp == -1 && errno == EAGAIN) 
    	{
       //  fprintf(stderr, "no client connections yet\n");
         usleep(100);
         continue;
      }   
    memcpy(Pclient.padress, inet_ntoa(clientaddr.sin_addr ), 20);
    printf("Pclient.padress %s\n", Pclient.padress);
      

    printf("Accepted client  [ip:%s port:%d]\n",inet_ntoa(clientaddr.sin_addr),  htons(clientaddr.sin_port));

    pthread_create(&sectid[i],NULL,createthread,&Pclient);
    usleep(500);
  }
  int m = 0;
  for( m = 0; m < maxsec; m++)
  {
    if(sectid[m] !=0) 
    {
    	printf("sectid  pthread_join .....\n");
       pthread_join(sectid[m], NULL);
       printf("sectid  pthread_join completed\n");
    } 
  }
  
  EXIT_SUCCESS;
}

void *createthread(void *vargp)
{
  pthread_t tid1,tid2;

  pthread_create(&tid1,NULL,threadsend,vargp);
  pthread_create(&tid2,NULL,threadrecv,vargp);
  if(tid1 !=0) 
  {
     pthread_join(tid1,NULL);
     printf("server threadsend  pthread_join completed\n");
  }
  if(tid2 !=0)
  {
     pthread_join(tid2, NULL);
     printf("server threadrecv  pthread_join completed\n");
  }
  printf("server createthread over \n");
  pthread_exit(0);
}

void *threadsend(void * vargp)
{

  int connfd = ((clieninfoT *)vargp)->connfdp;
  
  int idata;
  signed char temp[100];
  while(exit_flag)
  {
  	memset(temp, 0, sizeof(temp));
    fgets(temp, 100, stdin);
    if(strlen(temp) > 0)
    {
      send(connfd, temp, 100, 0);
      printf("             msg send OK\n");    	
    }
    usleep(500);
    if(memcmp(temp, "exit", 4) == 0)
    {
      exit_flag = 0; break;  		
    }    
  }
  printf("server send over and exit \n");
  pthread_exit(0);  
}

void *threadrecv(void *vargp)
{
  char temp[100];

  int connfd = ((clieninfoT *)vargp)->connfdp;
  while(exit_flag)
  {
  	memset(temp, 0, sizeof(temp));
  	int idata = 0;
  	if(!exit_flag) break;
    idata = recv(connfd,temp,100,0);
    if(idata > 0)
    {
      printf("client[%s] message: %s\n", ((clieninfoT *)vargp)->padress, temp);
    }
    usleep(500);    
  }
  printf("server threadrecv over and exit \n");
  pthread_exit(0);
}

ifconfig查看主机ip

运行cli 参数主机ip 1001

./cli 192.168.1.20 1001

2021.7.16快乐实验

上一篇:线程同步-互斥量


下一篇:C++服务器开发精髓笔记