有关eclipse+cdt+wingw5.1.4开发socket网络编程的问题

 最近一直想写个有关c语言的socket通信,我使用的是eclise+cdt+wingw5.1.4,server和client都创建成功了,就是在eclipse的控制台没有任何的输出。。大家有对eclipse开发c的感兴趣的帮忙看看吧。。谢谢了。(我写的代码如下)

 

//server.c 服务器端和客户端 彩信的是udp通信

 

#include "winsock2.h"
#include "stdio.h"
#pragma comment(lib , "ws2_32.lib")


//顶一个存储IP以及端口号的结构体
struct CLIENT_ADDR
{
 unsigned char s_b1 , s_b2 , s_b3 , s_b4;
 unsigned short int s_port;
};
//对定义的包含IP以及端口号的结构取个别名
typedef struct CLIENT_ADDR ClientAddr;
//创建一个CLIENT_ADDR结构体
ClientAddr *createClientAddr(unsigned char s_b1 , unsigned char s_b2 , unsigned char s_b3 , unsigned char  s_b4 , unsigned short int s_port)
{
 ClientAddr *clientAddr = NULL;
 clientAddr = (ClientAddr*)malloc(sizeof(ClientAddr));
 if(clientAddr==NULL)
  return NULL;
 clientAddr->s_b1 = s_b1;
 clientAddr->s_b2 = s_b2;
 clientAddr->s_b3 = s_b3;
 clientAddr->s_b4 = s_b4;
 clientAddr->s_port = s_port;
 return clientAddr;
}
//定义一个地址节点
struct ADDR_NODE
{
 struct CLIENT_ADDR *client_addr;
 struct ADDR_NODE *next;
};
//给地址节点取个别名
typedef struct ADDR_NODE AddrNode;
//创建一个子节点
AddrNode *createAddrNode(ClientAddr *clientAddr)
{
 AddrNode *addrNode = NULL;
 addrNode = (AddrNode*)malloc(sizeof(AddrNode));
 if(addrNode!=NULL)
 {
  addrNode->client_addr = clientAddr;
 }
 else
 {
  return NULL;
 }
}
//对链表的一个子节点进行删除
void deleteAddrNode(AddrNode *addrNode)
{
 if(addrNode!=NULL)
 {
  ClientAddr *clientAddr = addrNode->client_addr;
  if(clientAddr!=NULL)
  {
   free(clientAddr);
  }
  free(addrNode);
 }
}
//对整个链表进行删除
int deleteAddrNodes(AddrNode *head)
{
 if(head!=NULL)
 {
  AddrNode *curNode = NULL;
  do{
   curNode = head;
   head = head->next;
   deleteAddrNode(curNode);
  }while(head!=NULL);
 }
 else
  return 1;
}
//向链表中添加子节点
AddrNode * pushAddrNode(AddrNode *head , AddrNode *addrNode)
{
 if(head==NULL)
 {
  if(addrNode==NULL)
   return NULL;
  else return addrNode;
 }
 else
 {
  addrNode->next = head->next;
  head->next = addrNode;
  return head;
 }
}
//对链表中的节点进行统计
int getSize(AddrNode *head)
{
 if(head==NULL)
  return 0;
 AddrNode *curNode = NULL;
 curNode = head;
 int size = 0;
 while(curNode!=NULL)
 {
  size++;
  curNode = curNode->next;
 }
 return size;
}
//定义一个链表头
AddrNode *head = NULL;

int main()
{
 puts("服务器启动!");
 SOCKET serverSocket;
 WORD wVersionRequested;
 wVersionRequested = MAKEWORD(2 , 0);
 WSADATA wsaData;
 int err = 0;
 err = WSAStartup(wVersionRequested , &wsaData);
 printf("err : %d/n" ,err);
 SOCKADDR_IN server_addr;
 SOCKADDR_IN clientAddr;
 int sockAddr_size = sizeof(SOCKADDR);
 server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
 server_addr.sin_family = AF_INET;
 server_addr.sin_port = htons(6666);
 serverSocket = socket(AF_INET , SOCK_DGRAM , 0);
 if(serverSocket==INVALID_SOCKET)
 {
   printf("创建socket失败!/n");
 }
 //绑定
 err = bind(serverSocket , (SOCKADDR *)&server_addr ,sockAddr_size);
 if(err!=0)
 {
  printf("socket绑定失败!/n");
 }else
 {
  printf("socket绑定成功!/n");
 }
 int clientAddr_size = sizeof(SOCKADDR_IN);
 while(1)
 {
  char msg[100];
  err = recvfrom(serverSocket ,msg ,sizeof(msg) , 0 , (SOCKADDR *)&clientAddr , &clientAddr_size);
  printf("client : %s/n" , msg);
  if(err==-1)
  {
   puts("接收消息失败!");
   break;
  }
  char sendbuf[] = "消息收到了!";
  err = sendto(serverSocket ,sendbuf , strlen(sendbuf)+1 ,0 ,(SOCKADDR *)&clientAddr ,clientAddr_size);
  if(err==-1)
  {
   puts("发送消息失败!");
   break;
  }else
  {
   puts("发送消息成功!");
  }
  unsigned short int s_port = ntohs(clientAddr.sin_port);
  unsigned char s_b1 = clientAddr.sin_addr.S_un.S_un_b.s_b1;
  unsigned char s_b2 = clientAddr.sin_addr.S_un.S_un_b.s_b2;
  unsigned char s_b3 = clientAddr.sin_addr.S_un.S_un_b.s_b3;
  unsigned char s_b4 = clientAddr.sin_addr.S_un.S_un_b.s_b4;
  ClientAddr *clientAddr = createClientAddr(s_b1 , s_b2 , s_b3 ,s_b4 ,  s_port);
  AddrNode *addrNode = createAddrNode(clientAddr);
  printf("----------/n");
  printf(" %d" , clientAddr->s_b1);
  printf(" %d" , clientAddr->s_b2);
  printf(" %d" , clientAddr->s_b3);
  printf(" %d" , clientAddr->s_b4);
  printf(":%d/n" , clientAddr->s_port);
  printf("----------/n");
  pushAddrNode(head , addrNode);
  int size = getSize(head);
  printf("当前链表中的子节点的个数:%d/n" , size);

 }
 closesocket(serverSocket);
 WSACleanup();

 return 0;
}

//=======

//client.c

#include "stdio.h"
#include "winsock2.h"
#pragma comment(lib , "ws2_32.lib")

int main()
{
 puts("客户端启动!/n");
 SOCKET clientSocket;

 WORD wVersionRequested;
 wVersionRequested = MAKEWORD(2 , 0);
 WSADATA  wsaData;
 int err = 0;
 err = WSAStartup(wVersionRequested , &wsaData);
 if(err!=0)
 {
  printf("初始化失败!/n");
 }else
 {
  printf("初始化成功!/n");
 }
 //创建socket
 clientSocket = socket(AF_INET , SOCK_DGRAM , 0);
 SOCKADDR_IN  server_addr;
 server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
 server_addr.sin_family = AF_INET;
 server_addr.sin_port = htons(6666);
 int serverAddr_size = sizeof(SOCKADDR_IN);
 int msg_len = 0;
 while(1)
 {
  char msg[100];
  printf("请输入要发送的消息:/n");
  gets(msg);
  msg_len = strlen(msg);
  if(msg_len==0)
  {
   printf("发送的消息不能为空!/n");
  }else
  {
    err = sendto(clientSocket , msg , strlen(msg)+1, 0 , (SOCKADDR *)&server_addr ,serverAddr_size);
    if(err==-1)
    {
     puts("发送消息失败!");
     break;
    }
    char recvbuf[100];
    err = recvfrom(clientSocket , recvbuf , sizeof(recvbuf) ,0 , (SOCKADDR*)&server_addr , &serverAddr_size);
    if(err==-1)
    {
     puts("接收失败!");
     break;
    }else
    {
     printf("server : ");
     puts(recvbuf);
    }
  }
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值