📄 tcpser.c
字号:
/*******************************************************************************
File name: tcps1.c
Description:
This program demo
how to use socket lib coding TCP Server program on UC7000/DA-600 series.
The Hardware connection is following:
[UC7000/DA-600 series]--- ~~LAN~~ ---PC Host Example (Windows platform)
If data received from Ethernet,send them all back to the LAN port.
1. Create TCP/IP socket.
2. Socket Listen on TCP Port 4001.
3. If TCP connection is established, R/W data.
Usage:
1. Make downloading file (tcps1-release or tcps1-debug) for UC7000/DA-600 series
1.1 Create executable file from source code(*.c)
1.1.1 Modify tcps1.c:
change "TCPPORT" if needed.
1.1.2 Compiler and link to make tcps1-release (no debug message) or tcps1-debug (with debug message)
2. Download executable file to UC7000/DA-600 series
3. Run Application on UC7000/DA-600 series
4. Execute TCP Client example for PC.[tcpclient.exe or frmClient.exe]
input the TCP Port number equal to "TCPPORT"
UC7000/DA-600 series Server ip equal to UC7000/DA-600 series IP adddress.
5. user can check if the sended data received back.
History:
Version Author Date Comment
1.0 Victor Yu. 01-15-2004 Wrote it.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include"comlib.h"
#include"comlib.c"
#define TCPPORT 502
#define TESTLEN 1024
char buf[TESTLEN];
unsigned short ReadBuff[1024];
unsigned short WriteBuff[1024];
unsigned char TxBuff[400];
struct ModTcpRecv
{
unsigned char transaction_identifier[2];
unsigned char protocol_identifier[2];
unsigned char length_field_hi;
unsigned char length_field_lo;
unsigned char unit_identifier;
unsigned char function_code;
unsigned char ptr_hi;
unsigned char ptr_lo;
unsigned char len_hi;
unsigned char len_lo;
}*pModTcpRecv;
struct ModTcpSend
{
unsigned char transaction_identifier[2];
unsigned char protocol_identifier[2];
unsigned char length_field_hi;
unsigned char length_field_lo;
unsigned char unit_identifier;
unsigned char function_code;
unsigned char byte_count;
unsigned char buff[300];
}*pModTcpSend;
struct ModTcpWrite
{
unsigned char transaction_identifier[2];
unsigned char protocol_identifier[2];
unsigned char length_field_hi;
unsigned char length_field_lo;
unsigned char unit_identifier;
unsigned char function_code;
unsigned char ptr_hi;
unsigned char ptr_lo;
unsigned char nData_hi;
unsigned char nData_lo;
}*pModTcpWrite;
int ChangeHiLo(unsigned short *ptr,unsigned int len) //change unsigned data high byte and low byte to modscan look easy
{
int i;
unsigned char temp[2];
unsigned short old,new1;
for(i=0;i<len;i++)
{ old=*ptr;
temp[0]=(old & 0xff00) >>8;
temp[1]=(old & 0xff);
new1=temp[1]*256+temp[0];
*ptr=new1;
ptr++;
}
return(0);
}
int ModTcpDeal(unsigned char *pInBuff,unsigned char *pOutBuff)
{
unsigned char *pByte;
int i;
pModTcpRecv=(struct ModTcpRecv *)pInBuff;
pModTcpSend=(struct ModTcpSend *)pOutBuff;
pModTcpSend->transaction_identifier [0]=pModTcpRecv->transaction_identifier [0];
pModTcpSend->transaction_identifier [1]=pModTcpRecv->transaction_identifier [1];
pModTcpSend->protocol_identifier [0]=pModTcpRecv->protocol_identifier [0];
pModTcpSend->protocol_identifier [1]=pModTcpRecv->protocol_identifier [1];
pModTcpSend->length_field_hi =0x00;
pModTcpSend->length_field_lo =(unsigned char)(3+pModTcpRecv->len_lo *2) ;
if(0x03==pModTcpRecv->function_code )
{
pByte=(unsigned char *)WriteBuff+(pModTcpRecv->ptr_hi *256+pModTcpRecv->ptr_lo)*2 ;
pModTcpSend->unit_identifier =pModTcpRecv->unit_identifier ;
pModTcpSend->function_code =pModTcpRecv->function_code ;
pModTcpSend->byte_count =pModTcpRecv->len_lo *2;
for(i=0;i<pModTcpRecv->len_lo *2;i++)
{
pModTcpSend->buff [i]=*pByte;
pByte++;
}
ChangeHiLo((unsigned short *)pModTcpSend->buff,pModTcpRecv->len_lo);
return (sizeof(struct ModTcpSend)-300+i);
}
if(0x04==pModTcpRecv->function_code )
{
pByte=(unsigned char *)ReadBuff+(pModTcpRecv->ptr_hi *256+pModTcpRecv->ptr_lo)*2 ;
pModTcpSend->unit_identifier =pModTcpRecv->unit_identifier ;
pModTcpSend->function_code =pModTcpRecv->function_code ;
pModTcpSend->byte_count =pModTcpRecv->len_lo *2;
for(i=0;i<pModTcpRecv->len_lo *2;i++)
{
pModTcpSend->buff [i]=*pByte;
pByte++;
}
ChangeHiLo((unsigned short *)pModTcpSend->buff,pModTcpRecv->len_lo);
return (sizeof(struct ModTcpSend)-300+i);
}
if(0x06==pModTcpRecv->function_code )
{
pByte=(unsigned char *)WriteBuff+(pModTcpRecv->ptr_hi *256+pModTcpRecv->ptr_lo)*2 ;
pModTcpWrite=(struct ModTcpWrite *)pInBuff;
*(pByte+0)=pModTcpWrite->nData_lo ;
*(pByte+1)=pModTcpWrite->nData_hi;
for(i=0;i<sizeof(struct ModTcpWrite);i++)
{
*(pOutBuff+i)=*(pInBuff+i);
}
return sizeof(struct ModTcpWrite);
}
return 0;
//return 0;
}
int main()
{
int sListen, sClient;
int size,len,nRet,i;
struct sockaddr_in local, client;
//printf("sizeof(short)=%d\n",sizeof(short));
for(i=0;i<1024;i++) ReadBuff[i]=i+1;
for(i=0;i<1024;i++) WriteBuff[i]=i;
//ChangeHiLo(ReadBuff,i);
printf("create socket......");
if ( (sListen=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
{
printf("fail [%d]\n", errno);
return 0;
} else
printf("OK\n");
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(TCPPORT);
printf("socket bind......");
if ( bind(sListen, (struct sockaddr *)&local, sizeof(local)) < 0 )
{
printf("fail [%d]\n", errno);
return 0;
} else
printf("OK\n");
printf("socket listen......");
if (listen(sListen,1) < 0)
{
printf("fail [%d]\n", errno);
return 0;
} else
printf("OK\n");
size = sizeof(client);
printf("accept......");
if ( (sClient=accept(sListen, (struct sockaddr *)&client, &size)) < 0 )
{
printf("fail [%d]", errno);
return 0;
} else
printf("OK\n");
printf("ready to recv & write data\n");
while(1)
{
len = recv(sClient, buf, TESTLEN, 0);
if ( len > 0 )
{
PrintHexLog(stderr,buf,len);
//
nRet=ModTcpDeal((unsigned char *)buf,TxBuff);
if(nRet>0)
{
PrintHexLog(stderr,TxBuff,nRet);
/*
pModTcpRecv=(struct ModTcpRecv *)buf;
//pModTcpRecv->unit_identifier
printf("pModTcpRecv->unit_identifier =%X\n",pModTcpRecv->unit_identifier );
printf("pModTcpRecv->function_code=%X\n",pModTcpRecv->function_code);
printf("pModTcpRecv->ptr_hi =%X\n",pModTcpRecv->ptr_hi );
printf("pModTcpRecv->ptr_lo =%X\n",pModTcpRecv->ptr_lo );
printf("pModTcpRecv->len_hi =%X\n",pModTcpRecv->len_hi );
printf("pModTcpRecv->len_lo =%X\n",pModTcpRecv->len_lo );*/
//if (send(sClient,buf,len,0) <= 0)
if (send(sClient,TxBuff,nRet,0) <= 0)
{
printf("send fail [%d]\n", errno);
break;
}
}
}
else if ( len == 0 )
{
printf("disconnect\n");
break;
}
else if ( len < 0 )
{
printf("recv fail [%d]", errno);
break;
}
}
printf("close socket\n");
close(sListen);
close(sClient);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -