/************************************/
//头文件MySc.h:
#include<winsock.h>
#include<stdio.h>
#define version MAKEWORD(1,1)
#define port 5150
#define False 0
#define Ture 1
/*************************************/
//服务器端:Server.cpp
#include "MySc.h"
void main(void)
{
WSADATA wsaData;
SOCKET ServerSocket;
SOCKET AcceptSocket;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port=port;
int iReturn=0;
char RecvBuf[10];
int i;
//初始化为2.2版本
iReturn=WSAStartup(version,&wsaData);
if(iReturn!=0)
{
printf("ERROR11!");//ERROR
}
//创建一个套接字
ServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ServerSocket==INVALID_SOCKET)
{
printf("ERROR12!");//ERROR
}
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_port=htons(Port);
ServerAddr.sin_addr.s_addr=inet_addr("192.168.20.145");
//将地址端口信息与创建的套接字绑定起来
iReturn=bind(ServerSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));
if(iReturn==SOCKET_ERROR)
{
printf("ERROR13!");//ERROR
}
//监听套接字,监听客户端的连接情况
iReturn=listen(ServerSocket, 5);
if(iReturn==SOCKET_ERROR)
{
printf("ERROR14!");//ERROR
}
//接受一个连接请求
ClientAddrLen=sizeof(ClientAddrLen);
AcceptSocket=accept(ServerSocket, (SOCKADDR *)&ClientAddr, &ClientAddrLen);
if(AcceptSocket==INVALID_SOCKET)
{
printf("ERROR15!");//ERROR
}
//接受数据
iReturn=recv(AcceptSocket, RecvBuf, 10 ,0);
if(iReturn==SOCKET_ERROR)
{
printf("ERROR16!");//ERROR
}
else
for(i=0; i<10; i++)
{
printf("RECV %c",RecvBuf[i]);
}
//关闭套接字
closesocket(AcceptSocket);
closesocket(ServerSocket);
//释放套接字
WSACleanup();
}
/**************************************/
//客户端:Client.cpp
#include"MySc.h"
void main(void)
{
WSADATA wsaData;
SOCKET ClientSocket;
SOCKADDR_IN ServerAddr;
int Port=port;
char SendBuf[100];
int iReturn;
int i;
iReturn=WSAStartup(version,&wsaData);
if(iReturn!=0)
{
printf("ERROR1!");//ERROR
}
//创建一个套接字
ClientSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ClientSocket==INVALID_SOCKET)
{
printf("ERROR2!");//ERROR
}
//bzero(&ServerAddr.sin_zero,sizeof(ServerAddr.sin_zero));
//建立一个SOCKADDR_IN结构用于在Port端口连接一正在监听的服务器
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_port=htons(Port);
ServerAddr.sin_addr.s_addr=inet_addr("192.168.20.145");//服务器的IP地址
//建立和服务器的连接
iReturn=connect(ClientSocket, (sockaddr *)&ServerAddr, sizeof(ServerAddr));
if(iReturn==SOCKET_ERROR)
{
printf("ERROR3!");//ERROR
}
//发送数据
//SendBuf[10]="123456789";
for(i=0; i<10; i++)
SendBuf[i]=i;
iReturn=send(ClientSocket,SendBuf,10,0);
if(iReturn==SOCKET_ERROR)
{
printf("ERROR4!");//ERROR
}
//关闭套接字
closesocket(ClientSocket);
//释放套接字
WSACleanup();
}
----------------解决方案--------------------------------------------------------
#define Server_port 5150
#define Client_port 5151
----------------解决方案--------------------------------------------------------
不明白.愿意给我讲讲课吗??
----------------解决方案--------------------------------------------------------
问题解决了,呵呵
是我的工作环境设置得有问题
端口设置没有问题
----------------解决方案--------------------------------------------------------