不知道什么原因,可能vxworks的某个组件没加、BSP不完善、板卡问题?我现在碰到个问题,困恼了我好久依旧没解决——vxworks服务器端一直阻塞在accept处,windows客户端在connect后返回为-1,两者连不上。
(我在vxworks(ARM2440板卡上)中加载了socket服务器程序,在win下运行自己编的客户端程序,另外,我在win下也编了个服务器程序,win下的客户端可以和win下的服务器正常通信。)
串口打印信息为:
0x33f805ac (tDbgTask): This is just server_socket test ...
after socket return 4
after bind return 0
after listen return 0
在shell下 ifShow后显示为:
-> ifShow
dmf (unit number 0):
Flags: (0x8863) UP BROADCAST RUNNING ARP MULTICAST
Type: ETHERNET_CSMACD
Internet address: 192.168.0.99
Broadcast address: 192.168.0.255
Netmask 0xffffff00 Subnetmask 0xffffff00
Ethernet address is 1a:2b:3c:4d:5e:6f
Metric is 0
Maximum Transfer Unit size is 1500
58 packets received; 6 packets sent
46 multicast packets received
1 multicast packets sent
0 input errors; 0 output errors
0 collisions; 0 dropped
lo (unit number 0):
Flags: (0x8069) UP LOOPBACK RUNNING ARP MULTICAST
Type: SOFTWARE_LOOPBACK
Internet address: 127.0.0.1
Netmask 0xff000000 Subnetmask 0xff000000
Metric is 0
Maximum Transfer Unit size is 32768
0 packets received; 0 packets sent
0 multicast packets received
0 multicast packets sent
0 input errors; 0 output errors
0 collisions; 0 dropped
value = 0 = 0x0
在PC上抓包为:
[IMG]http://images.blogcn.com/2006/12/20/10/sunbin1010,20061220191756.jpg[/IMG]
[IMG]http://images.blogcn.com/2006/12/20/12/sunbin1010,2006122023547.jpg[/IMG]
[IMG]http://images.blogcn.com/2006/12/20/10/sunbin1010,2006122019417.jpg[/IMG]
下面分别是vxworks(server)、windows(client)下的源码:
vxworks:
#include "vxWorks.h"
#include "sockLib.h"
#include "taskLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "ioLib.h"
#include "sioLib.h"
#include "string.h"
#include "stdLib.h"
#include "stdio.h"
#define MYPORT 2001
#define BACKLOG 3
int serverfunc(void)
{
char buf[1024];
char * clientip;
struct sockaddr_in client;
struct sockaddr_in server;
int sk;
int newsk;
int namelen;
int pktlen;
int temp1,temp2,temp3;
logMsg("%s\n\n","This is just server_socket test ...",0,0,0,0,0);
if ((sk=socket(AF_INET, SOCK_STREAM, 0))<0)
{
logMsg("%s","Socket build fail",0,0,0,0,0);
return (-1);
}
printf("\nafter socket return %d\n",sk);
memset((char *)&server,'\0',sizeof(server));
server.sin_family = AF_INET;
server.sin_len = sizeof(server);
server.sin_port = htons(MYPORT);
server.sin_addr.s_addr = INADDR_ANY;
if ((temp2=bind(sk, (struct sockaddr *)&server, sizeof(server)))==-1)
{
logMsg("%s","Bind() fail",0,0,0,0,0);
return (-1);
}
printf("\nafter bind return %d\n",temp2);
if ((temp3=listen(sk,BACKLOG))==-1)
{
logMsg("%s","Listen() fail",0,0,0,0,0);
return (-1);
}
printf("\nafter listen return %d\n",temp3);
namelen = sizeof(client);
if ((newsk = accept(sk, (struct sockaddr *)&client, &namelen)) ==-1)
{
logMsg("%s","Accept() fail",0,0,0,0,0);
printf("Accept() fail");
return (-1);
}
clientip=inet_ntoa(client.sin_addr);
logMsg("Got connect from: %s\n",clientip,0,0,0,0,0);
printf("\nnew fs is:%d\n",newsk);
while(1)
{
if ((pktlen = recv(newsk,buf,1024,0))<0)
{
logMsg("%s","Recv()",0,0,0,0,0);
break;
}
else
if (pktlen == 0)
{
logMsg("\n%s\n\n","Connection is shut down!\n\n",0,0,0,0,0);
break;
}
else
{
buf[pktlen]='\0';
logMsg("\n\"%s\" said: %s",clientip,buf,0,0,0,0);
}
}
close(newsk);
close(sk);
return 0;
}
windows:
#include <stdio.h>
#include <winsock.h>
#include "string.h"
#pragma comment(lib,"Ws2_32")
#define MAXSIZE 2048
#define sendport 2001
int argc=2;
struct sockaddr_in ClientAddr;
SOCKET sock;
DWORD startSock()
{
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
printf("sock init fail");
return(-1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
return 1;
}
int client(int argc, char *argv[])
{
u_int numbyte;
char buf[MAXSIZE];
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if(startSock()==1)
{
printf("\nStart Socket successfully!\n");
}
else
{
printf("Error!");
return 0;
}
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = htons(sendport);
ClientAddr.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sock, (struct sockaddr *)&ClientAddr,sizeof(struct sockaddr))==0)
{
printf("\nConnect successfully!\n");
printf("\nThe port number to send: %d",sendport);
printf("\nThe IP to send: %s\n",argv[1]);
}
else
{
printf("\nConnect unsuccessfully!\n");
return 0;
}
getchar();
while(1)
{
printf("\nPlease input the data you want send: ");
gets(buf);
if(strcmp(buf, "quit") == 0 )
{
closesocket(sock);
return 0;
}
int iLen = strlen(buf);
buf[iLen] = 0xa;
buf[iLen+1] = '\0';
numbyte=send(sock, buf, strlen(buf), 0);
if(numbyte==SOCKET_ERROR)
{
printf("\nSend error!\n");
closesocket(sock);
break;
}
}
return 0;
}
void main()
{
char *argv[2],hostname[10],hostip[15];
argv[0]=hostname;
argv[1]=hostip;
printf("This is just client_socket test ! Input \"quit\" to exit...\n\n");
// printf("Input host name: ");
// scanf("%s",argv[0]);
printf("\nInput host IP: ");
scanf("%s",argv[1]);
puts("\n");
client(argc,argv);
}