菜鸟请教:Socket传输的问题,服务端用C写的,如何用Delphi接受,拜谢了! ( 积分: 200 )

  • 主题发起人 主题发起人 xlz
  • 开始时间 开始时间
X

xlz

Unregistered / Unconfirmed
GUEST, unregistred user!
服务端代码:
#include "xyjh.h"
#include "cgasnet.h"
#define XZCBSJ 01
#define DRCBSJ 02
char cbjfilename[80];
int closefd, cbyh, zbs;
long rq;
main(argc,argv)
char *argv[];
int argc;
{
int srvport;
signal( SIGINT , SIG_IGN ) ;
signal( SIGHUP , SIG_IGN ) ;
signal( SIGCLD , SIG_IGN ) ;
if (argc!=2) {
printf("/n/n启动格式错误!/n/n");
exit(0);
}
srvport=atol(argv[1]);
if( fork() == 0 )
sub_main(srvport);
else
exit( 0 );
}

sub_main(port)
int port;
{
struct sockaddr_in s_add , con_s_add ;
struct servent *p_servent ;
int sockfd , new_sockfd ;
pid_t child_pid ;
int addr_len ;
int bind(),accept();


sockfd = socket ( AF_INET , SOCK_STREAM , IPPROTO_IP );
if ( sockfd < 0 )
{
perror ( &quot; socket call error: &quot; );
exit ( -1 );
}

s_add.sin_family = AF_INET ;
/*s_add.sin_port = htons( SRVPORT ) ;*/
s_add.sin_port = htons( port ) ;
s_add.sin_addr.s_addr = htonl( INADDR_ANY ) ;
if( bind ( sockfd , &amp;s_add , sizeof( s_add ) ) < 0 )
{
perror ( &quot; bind call error: &quot;);
exit ( -1 );
}

if ( listen ( sockfd , 5 ) < 0 )
{
perror ( &quot;listen call error: &quot; );
exit ( -1 );
}

addr_len = sizeof (con_s_add ) ;

printf ( &quot;启动成功 ! /n/n&quot; ) ;
while( 1 )
{
new_sockfd = accept ( sockfd , &amp;con_s_add , &amp;addr_len );

if ( new_sockfd < 0 )
{
perror ( &quot; accept call error: &quot; );
exit ( -1 );
}

if ( ( child_pid = fork() ) == 0 )
{
close ( sockfd );
sjjh( new_sockfd );
close ( new_sockfd );
exit( 0 );
}
else if ( child_pid > 0 )
close ( new_sockfd ) ;
else
close ( new_sockfd ) ;
/*sjjh ( new_sockfd );*/
}
}


sjjh(fd)
int fd;
{
int flag, FirstReadLen;
char pkl[20], jym[20], syhbh[20], tbtmp[512];
char frombank[FROMBANKLEN], tobank[TOBANKLEN];
char exestr[512];
char str[512];
void TIMEOUTread(), TIMEOUTwrite();
closefd=fd;
signal(SIGALRM,TIMEOUTread);
alarm(TIMEOUT);
FirstReadLen = read(fd, frombank, FROMBANKLEN);
if ( FirstReadLen <= 0 ) {
signal(SIGALRM,SIG_IGN);
return ;
}
signal(SIGALRM,SIG_IGN);
ltrim(frombank);
dzrtrim(frombank);
errout(&quot;银行上传&quot;,frombank,__FILE__,__LINE__,0);
getfield(frombank,1,pkl); ltrim(pkl); rtrim(pkl);
getfield(frombank,2,jym); ltrim(jym); rtrim(jym);
getfield(frombank,3,syhbh); ltrim(syhbh); rtrim(syhbh);
switch(atoi(jym)){
case XZCBSJ:
flag=xzcbsj(frombank,tobank);
if (flag==0)
sprintf(exestr,&quot;sxzcbsj %d %d %ld&quot;,fd,cbyh,rq);
break;
case DRCBSJ:
flag=drcbsj(frombank,tobank,FirstReadLen);
if (flag==0)
sprintf(exestr,&quot;sdrcbsj %d %d %ld %d&quot;,fd,cbyh,rq,zbs);
default :
flag=-1;
sprintf(tobank,&quot;31|&quot;);
break;
}
if (flag) {
strcpy(tbtmp,tobank); rtrim(tbtmp);
sprintf(tobank,&quot;%04d|%s&quot;,strlen(tbtmp)+5,tbtmp);
rtrim(tobank);
signal(SIGALRM,TIMEOUTwrite);
alarm(TIMEOUT);
flag =write ( fd, tobank, strlen(tobank) ) ;
errout(&quot;发送银行&quot;,tobank,__FILE__,__LINE__,0);
signal(SIGALRM,SIG_IGN);
/*close(fd);*/
}else{
rtrim(exestr);
flag=system(exestr);
}
/*if (flag<=0) autocz(jym);*/
}
void TIMEOUTwrite()
{
close(closefd);
errout(&quot;发送超时&quot;,&quot;&quot;,__FILE__,__LINE__,closefd);
exit(-1);
}
void TIMEOUTread()
{
close(closefd);
errout(&quot;接收超时&quot;,&quot;&quot;,__FILE__,__LINE__,closefd);
exit(-1);
}
int xzcbsj(f_bankcx,t_bankcx)
char *f_bankcx, *t_bankcx;
{
char cbystr[20], rqstr[20], pkl[20], jymstr[20];
getfield(f_bankcx,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankcx,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankcx,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankcx,4,rqstr); rtrim(rqstr); ltrim(rqstr);
if (checkdig(cbystr)) {
errout(&quot;抄表员字段有误!&quot;,cbystr,__FILE__,__LINE__,999);
sprintf(f_bankcx,&quot;37|&quot;);
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&amp;rq,&quot;yyyymmdd&quot;,rqstr)){
errout(&quot;时期格式有误!&quot;,rqstr,__FILE__,__LINE__,999);
sprintf(f_bankcx,&quot;37|&quot;);
return -1;
}
cbyh = atoi(cbystr);
sprintf(t_bankcx,&quot;98|&quot;);
return 0;
}

int drcbsj(f_bankdz,t_bankdz, FirstRead)
char *f_bankdz, *t_bankdz;
int FirstRead;
{
int Ixym, i;
long FileLen, len;
char rqstr[20], jymstr[20], bsstr[20], cbystr[20], pkl[20], str[20];
char *data;
FILE *fp;
getfield(f_bankdz,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankdz,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankdz,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankdz,4,rqstr); rtrim(rqstr); ltrim(rqstr);
getfield(f_bankdz,5,bsstr); rtrim(bsstr); ltrim(bsstr);
if (checkdig(bsstr)) {
errout(&quot;记录数字段有误!&quot;,bsstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
if (checkdig(cbystr)) {
errout(&quot;抄表员字段有误!&quot;,cbystr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&amp;rq,&quot;yyyymmdd&quot;,rqstr)){
errout(&quot;时期格式有误!&quot;,rqstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
zbs = atoi(bsstr);
cbyh = atoi(cbystr);
sprintf(cbjfilename,&quot;%s/cbj%04d%s.txt&quot;, getenv(&quot;CBJDIR&quot;),cbyh,rqstr);
if ( (fp=fopen(cbjfilename,&quot;wb&quot;))==NULL ){
fclose(fp);
errout(&quot;打开写文件失败!&quot;,cbjfilename,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;98|&quot;);
return -1;
}
fwrite(f_bankdz,FirstRead,1,fp); /* 写入第一包 */

FileLen = atol( pkl );
len = FileLen-FirstRead;
if ( len <=0 )
{
fclose(fp);
sprintf(t_bankdz,&quot;98|&quot;);
return 0;
}
data = ( char * ) malloc ( len ) ;
for ( i = 0; i < len; i++ ) data = 0;
i=readn ( closefd, data, len );
if ( i>0 )
fwrite(data,len,1,fp);
fclose(fp);

sprintf(t_bankdz,&quot;98|&quot;);
return 0;
}

int
readn ( int fd, char *ptr, long nbytes)
{

long nleft, nread;
int sockfd;
sockfd = fd;

nleft = nbytes;
while ( nleft > 0 ) {
nread = read (fd, ptr, nleft );
if ( nread < 0 )
return ( nread );
else
if ( nread == 0 )
break;
nleft -= nread;
ptr += nread;
}

return nbytes - nleft ;
}
 
服务端代码:
#include &quot;xyjh.h&quot;
#include &quot;cgasnet.h&quot;
#define XZCBSJ 01
#define DRCBSJ 02
char cbjfilename[80];
int closefd, cbyh, zbs;
long rq;
main(argc,argv)
char *argv[];
int argc;
{
int srvport;
signal( SIGINT , SIG_IGN ) ;
signal( SIGHUP , SIG_IGN ) ;
signal( SIGCLD , SIG_IGN ) ;
if (argc!=2) {
printf(&quot;/n/n启动格式错误!/n/n&quot;);
exit(0);
}
srvport=atol(argv[1]);
if( fork() == 0 )
sub_main(srvport);
else
exit( 0 );
}

sub_main(port)
int port;
{
struct sockaddr_in s_add , con_s_add ;
struct servent *p_servent ;
int sockfd , new_sockfd ;
pid_t child_pid ;
int addr_len ;
int bind(),accept();


sockfd = socket ( AF_INET , SOCK_STREAM , IPPROTO_IP );
if ( sockfd < 0 )
{
perror ( &quot; socket call error: &quot; );
exit ( -1 );
}

s_add.sin_family = AF_INET ;
/*s_add.sin_port = htons( SRVPORT ) ;*/
s_add.sin_port = htons( port ) ;
s_add.sin_addr.s_addr = htonl( INADDR_ANY ) ;
if( bind ( sockfd , &amp;s_add , sizeof( s_add ) ) < 0 )
{
perror ( &quot; bind call error: &quot;);
exit ( -1 );
}

if ( listen ( sockfd , 5 ) < 0 )
{
perror ( &quot;listen call error: &quot; );
exit ( -1 );
}

addr_len = sizeof (con_s_add ) ;

printf ( &quot;启动成功 ! /n/n&quot; ) ;
while( 1 )
{
new_sockfd = accept ( sockfd , &amp;con_s_add , &amp;addr_len );

if ( new_sockfd < 0 )
{
perror ( &quot; accept call error: &quot; );
exit ( -1 );
}

if ( ( child_pid = fork() ) == 0 )
{
close ( sockfd );
sjjh( new_sockfd );
close ( new_sockfd );
exit( 0 );
}
else if ( child_pid > 0 )
close ( new_sockfd ) ;
else
close ( new_sockfd ) ;
/*sjjh ( new_sockfd );*/
}
}


sjjh(fd)
int fd;
{
int flag, FirstReadLen;
char pkl[20], jym[20], syhbh[20], tbtmp[512];
char frombank[FROMBANKLEN], tobank[TOBANKLEN];
char exestr[512];
char str[512];
void TIMEOUTread(), TIMEOUTwrite();
closefd=fd;
signal(SIGALRM,TIMEOUTread);
alarm(TIMEOUT);
FirstReadLen = read(fd, frombank, FROMBANKLEN);
if ( FirstReadLen <= 0 ) {
signal(SIGALRM,SIG_IGN);
return ;
}
signal(SIGALRM,SIG_IGN);
ltrim(frombank);
dzrtrim(frombank);
errout(&quot;银行上传&quot;,frombank,__FILE__,__LINE__,0);
getfield(frombank,1,pkl); ltrim(pkl); rtrim(pkl);
getfield(frombank,2,jym); ltrim(jym); rtrim(jym);
getfield(frombank,3,syhbh); ltrim(syhbh); rtrim(syhbh);
switch(atoi(jym)){
case XZCBSJ:
flag=xzcbsj(frombank,tobank);
if (flag==0)
sprintf(exestr,&quot;sxzcbsj %d %d %ld&quot;,fd,cbyh,rq);
break;
case DRCBSJ:
flag=drcbsj(frombank,tobank,FirstReadLen);
if (flag==0)
sprintf(exestr,&quot;sdrcbsj %d %d %ld %d&quot;,fd,cbyh,rq,zbs);
default :
flag=-1;
sprintf(tobank,&quot;31|&quot;);
break;
}
if (flag) {
strcpy(tbtmp,tobank); rtrim(tbtmp);
sprintf(tobank,&quot;%04d|%s&quot;,strlen(tbtmp)+5,tbtmp);
rtrim(tobank);
signal(SIGALRM,TIMEOUTwrite);
alarm(TIMEOUT);
flag =write ( fd, tobank, strlen(tobank) ) ;
errout(&quot;发送银行&quot;,tobank,__FILE__,__LINE__,0);
signal(SIGALRM,SIG_IGN);
/*close(fd);*/
}else{
rtrim(exestr);
flag=system(exestr);
}
/*if (flag<=0) autocz(jym);*/
}
void TIMEOUTwrite()
{
close(closefd);
errout(&quot;发送超时&quot;,&quot;&quot;,__FILE__,__LINE__,closefd);
exit(-1);
}
void TIMEOUTread()
{
close(closefd);
errout(&quot;接收超时&quot;,&quot;&quot;,__FILE__,__LINE__,closefd);
exit(-1);
}
int xzcbsj(f_bankcx,t_bankcx)
char *f_bankcx, *t_bankcx;
{
char cbystr[20], rqstr[20], pkl[20], jymstr[20];
getfield(f_bankcx,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankcx,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankcx,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankcx,4,rqstr); rtrim(rqstr); ltrim(rqstr);
if (checkdig(cbystr)) {
errout(&quot;抄表员字段有误!&quot;,cbystr,__FILE__,__LINE__,999);
sprintf(f_bankcx,&quot;37|&quot;);
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&amp;rq,&quot;yyyymmdd&quot;,rqstr)){
errout(&quot;时期格式有误!&quot;,rqstr,__FILE__,__LINE__,999);
sprintf(f_bankcx,&quot;37|&quot;);
return -1;
}
cbyh = atoi(cbystr);
sprintf(t_bankcx,&quot;98|&quot;);
return 0;
}

int drcbsj(f_bankdz,t_bankdz, FirstRead)
char *f_bankdz, *t_bankdz;
int FirstRead;
{
int Ixym, i;
long FileLen, len;
char rqstr[20], jymstr[20], bsstr[20], cbystr[20], pkl[20], str[20];
char *data;
FILE *fp;
getfield(f_bankdz,1,pkl); rtrim(pkl); ltrim(pkl);
getfield(f_bankdz,2,jymstr); rtrim(jymstr); ltrim(jymstr);
getfield(f_bankdz,3,cbystr); rtrim(cbystr); ltrim(cbystr);
getfield(f_bankdz,4,rqstr); rtrim(rqstr); ltrim(rqstr);
getfield(f_bankdz,5,bsstr); rtrim(bsstr); ltrim(bsstr);
if (checkdig(bsstr)) {
errout(&quot;记录数字段有误!&quot;,bsstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
if (checkdig(cbystr)) {
errout(&quot;抄表员字段有误!&quot;,cbystr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
if (strlen(rqstr)!=8||rdefmtdate(&amp;rq,&quot;yyyymmdd&quot;,rqstr)){
errout(&quot;时期格式有误!&quot;,rqstr,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;37|&quot;);
return -1;
}
zbs = atoi(bsstr);
cbyh = atoi(cbystr);
sprintf(cbjfilename,&quot;%s/cbj%04d%s.txt&quot;, getenv(&quot;CBJDIR&quot;),cbyh,rqstr);
if ( (fp=fopen(cbjfilename,&quot;wb&quot;))==NULL ){
fclose(fp);
errout(&quot;打开写文件失败!&quot;,cbjfilename,__FILE__,__LINE__,999);
sprintf(t_bankdz,&quot;98|&quot;);
return -1;
}
fwrite(f_bankdz,FirstRead,1,fp); /* 写入第一包 */

FileLen = atol( pkl );
len = FileLen-FirstRead;
if ( len <=0 )
{
fclose(fp);
sprintf(t_bankdz,&quot;98|&quot;);
return 0;
}
data = ( char * ) malloc ( len ) ;
for ( i = 0; i < len; i++ ) data = 0;
i=readn ( closefd, data, len );
if ( i>0 )
fwrite(data,len,1,fp);
fclose(fp);

sprintf(t_bankdz,&quot;98|&quot;);
return 0;
}

int
readn ( int fd, char *ptr, long nbytes)
{

long nleft, nread;
int sockfd;
sockfd = fd;

nleft = nbytes;
while ( nleft > 0 ) {
nread = read (fd, ptr, nleft );
if ( nread < 0 )
return ( nread );
else
if ( nread == 0 )
break;
nleft -= nread;
ptr += nread;
}

return nbytes - nleft ;
}
 
已经解决,数据掉包
 
我今天才看到

夏在你的程序看看
 
缓冲一下接收包,处理的另外,一般不会掉包.
 
后退
顶部