#include <stdio.h>凳顷
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
//初始化WSA
WORD sockVersion = MAKEWORD(2,2)
WSADATA wsaData
if(WSAStartup(sockVersion, &wsaData)!=0)
{
return 0
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
if(slisten == INVALID_SOCKET)
{
printf("socket error !")
return 0
}
//绑定IP和端口
sockaddr_in sin
sin.sin_family = AF_INET
sin.sin_port = htons(8888)
sin.sin_addr.S_un.S_addr = INADDR_ANY
if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("bind error !")
}
//开始监听
if(listen(slisten, 5) == SOCKET_ERROR)
{
printf("listen error !")
return 0
}
//循环接收数据
SOCKET sClient
衫粗桥sockaddr_in remoteAddr
int nAddrlen = sizeof(remoteAddr)
char revData[255]
while (true)
{
printf("等待连接...\n")
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen)
if(sClient == INVALID_SOCKET)
{
printf("accept error !")
continue
}
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr))
//接收数据
int ret = recv(sClient, revData, 255, 0)
if(ret > 0)
{
revData[ret] = 0x00
printf(revData)
}
//发送数据
const char * sendData = "你好,TCP客户端!\n"
send(sClient, sendData, strlen(sendData), 0)
closesocket(sClient)
}
closesocket(slisten)
WSACleanup()
return 0
}
//客户端:client.cpp
#include<WINSOCK2.H>
#include<STDIO.H>
#include<iostream>
#include<cstring>
using namespace std
#pragma comment(lib, "ws2_32.lib")
int main()
{ 或猛
WORD sockVersion = MAKEWORD(2, 2)
WSADATA data
if(WSAStartup(sockVersion, &data)!=0)
{
return 0
}
while(true){
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
if(sclient == INVALID_SOCKET)
{
printf("invalid socket!")
return 0
}
sockaddr_in serAddr
serAddr.sin_family = AF_INET
serAddr.sin_port = htons(8888)
serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1")
if(connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{ //连接失败
printf("connect error !")
closesocket(sclient)
return 0
}
string data
cin>>data
const char * sendData
sendData = data.c_str() //string转const char*
//char * sendData = "你好,TCP服务端,我是客户端\n"
send(sclient, sendData, strlen(sendData), 0)
//send()用来将数据由指定的socket传给对方主机
//int send(int s, const void * msg, int len, unsigned int flags)
//s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0
//成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error
char recData[255]
int ret = recv(sclient, recData, 255, 0)
if(ret>0){
recData[ret] = 0x00
printf(recData)
}
closesocket(sclient)
}
WSACleanup()
return 0
}
首先蓝牙功能是无线的但是也有定距离你先将蓝牙蠢桐功能打开,然后他也将蓝牙功能打开
你点搜索那项找到他的机子,然后连接他的,会提示你输入没手个密码,你随便输个,111也行,他那就有个确认密码,输入111,就连上了,
以下是全部 *** 作
找到你要传的JAR,然后选项更多,选发送,通过BLUETOOTH发送枯档嫌,然后选到他的手机就发了,他只用点确认接受就行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)