⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.c

📁 TCP/IP 编程方面的编程在此。这里面一个用于服务器的编程
💻 C
字号:
/*-----------------------------------------------------------
This function realize the simple data sending process by 
using the tcp/ip protocal here. 
------------------------------------------------------------*/

#include <csl_emachal.h>
#include <csl_emac.h>
#include <csl_mdio.h>
#include <csl_mdio.h>
#include <csl_chip.h>

#include <stdio.h>
#include <stdlib.h>
//#include <Winsock2.h> 
//#include <socket.h>
#include "Server.h"
#include "MyDefine.h"
#include "Function_TCP.h"
 
#define DEFAULT_PORT 5050
//#define DATA_BUFFER 1024
  
void MainServer()
{              
  int iPort=DEFAULT_PORT;
                          //WSADATA wsaData;
  SOCKET sListen,sAccept;  
   
  //client address length 
  int iLen;
  
  // sending data length 
  int iSend;  
                                        // receiving data length
                                        // int iRecv;  
  //messeger to client
  char buf[]="Hello, I am a DSP server!";  
                                      
  //server address and client address
  struct sockaddr_in ser, cli;
  
  printf("-------------------");
  printf(" ---  Server waiting --- \n");
  printf("-------------------");
  
  //generate server socket 
  sListen=socket(AF_INET,SOCK_STREAM,0);
  if(sListen==INVALID_SOCKET)
  {
    printf("socket() failed: \n");
    return;
  }
    
  /*----in the following buildup server address----*/
  ser.sin_family=AF_INET;
    
  //htons() function transfer a double byte sequence number of a host computer 
  // to internet byte sequence number
  ser.sin_port=htons(iPort);
  
  //htonl() function transfer a 4 bytes sequence number of a host computer 
  //to internet byte sequence number  
  //use the system set IP address INADDR_ANY
  ser.sin_addr.s_addr=htonl(INADDR_ANY);
  
  /*if(bind(sListen,(LPSOCKADDR)&ser, sizeof(ser))==SOCKET_ERROR)
  {
    printf("bind() failed:\n");
    return;
  } */
  
  /*--enter into the listening state--*/
  if(listen(sListen,5)==SOCKET_ERROR)
  {
    printf("listen() failed: %d\n");
    return;
  }
  
  //initialize client address length parameter 
  iLen=sizeof(cli);        
                                               //initialize the receiving buffer
                                               //memset(recv_buf,o, sizeof(recv_buf);
  /*---enter into a limitless loop, wait for client request here---*/
  while(1)
  {
    sAccept=accept(sListen,(struct sockaddr*)&cli, &iLen);
    if(sAccept==INVALID_SOCKET)
    {
      printf("accept() failed:\n");
      break;
    }
    
    /*---output client IP address and port number---*/
    printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
    
    /*---send information to connected client here---*/
    iSend=send(sAccept,buf, sizeof(buf),0);
    if
    (iSend==SOCKET_ERROR)
    {
      printf("send() failed \n");
      break;
    }
    else if(iSend==0)
      break;
      else
      {
        printf("send() byte: %d\n", iSend);
        printf("----------------------------");   
      }
      closesocket(sAccept);
   }
   
    closesocket(sListen);
 }
    
        
    
    
       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -