如何用SOCKET实现两台计算机之间的通讯
用VC.利用vc++的Socket编程实现网络环境下两台计算机之间的字符聊天功能给一个聊天窗口,可以传送文字信息就好,请帮忙给出具体程序,最好能运行...
用VC.
利用vc++的Socket 编程实现网络环境下两台计算机之间的字符聊天功能
给一个聊天窗口,可以传送文字信息就好,请帮忙给出具体程序,最好能运行 展开
利用vc++的Socket 编程实现网络环境下两台计算机之间的字符聊天功能
给一个聊天窗口,可以传送文字信息就好,请帮忙给出具体程序,最好能运行 展开
3个回答
展开全部
两部分代码,一个server,一个client。
server 端代码:
#include "stdio.h"
#include "Winsock2.h"
#include "iostream"
#include "string"
//#pragma comment(lib, "ws2_32.lib")
//define host IP and usable port.
#define HOST_IP 127.0.0.1
#define HOST_PORT 8080
#define OK_STR "OK"
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int err;
err = WSAStartup(versionRequest, &wsaData);
if(err != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
SOCKET socServer = socket(AF_INET, SOCK_DGRAM,0);
SOCKADDR_IN addr_Srv;
//It's nest three structs as S_un_b, S_un_w and S-un.(union)
addr_Srv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//set protocol family
addr_Srv.sin_family = AF_INET;
//set host port
addr_Srv.sin_port = htons(HOST_PORT);
//bind socket to the host
bind(socServer,( SOCKADDR*) &addr_Srv, sizeof(SOCKADDR));
//address of a client
SOCKADDR_IN addr_Clt;
char recvBuf[100];
int fromlen = sizeof(SOCKADDR);
while(true){
recvfrom(socServer, recvBuf, 100, 0, (SOCKADDR*) &addr_Clt, &fromlen); std::cout<<recvBuf<<std::endl;
sendto(socServer, OK_STR, strlen(OK_STR)+1, 0, (SOCKADDR*)&addr_Clt,sizeof(SOCKADDR));
}
closesocket(socServer);
WSACleanup();
}
client端代码:
#include "Winsock2.h"
#include "iostream"
#include "stdio.h"
#pragma comment(lib, "ws2_32.lib")
#define HOST_IP "127.0.0.1"
#define HOST_PORT 8080
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int error;
error = WSAStartup(versionRequest, &wsaData);
if(error != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
//request info obtained by user's input
char requestStr[100];
//build a sockeet
SOCKET socClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET; //set the protocol family
addrSrv.sin_port=htons(HOST_PORT); //set the port number
// array to store the data that server feedback.
char cRecvBuf[100];
while(true){
std::cin>>requestStr;
//sendto function is used to send programe data to the server
sendto(socClient, requestStr, strlen(requestStr)+1, 0, (SOCKADDR*) &addrSrv, sizeof(SOCKADDR));
recv(socClient, cRecvBuf, strlen(cRecvBuf)+1, 0);
std::cout<< cRecvBuf << std::endl;
}
//close the socket and cleanup the wsadata
closesocket(socClient);
WSACleanup();
// return 0;
}
展开全部
两部分代码,一个server,一个client。
server
端代码:
#include "stdio.h"
#include "Winsock2.h"
#include "iostream"
#include "string"
//#pragma comment(lib, "ws2_32.lib")
//define host IP and usable port.
#define HOST_IP 127.0.0.1
#define HOST_PORT 8080
#define OK_STR "OK"
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int err;
err = WSAStartup(versionRequest, &wsaData);
if(err != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
SOCKET socServer = socket(AF_INET, SOCK_DGRAM,0);
SOCKADDR_IN addr_Srv;
//It's nest three structs as S_un_b, S_un_w and S-un.(union)
addr_Srv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//set protocol family
addr_Srv.sin_family = AF_INET;
//set host port
addr_Srv.sin_port = htons(HOST_PORT);
//bind socket to the host
bind(socServer,( SOCKADDR*) &addr_Srv, sizeof(SOCKADDR));
//address of a client
SOCKADDR_IN addr_Clt;
char recvBuf[100];
int fromlen = sizeof(SOCKADDR);
while(true){
recvfrom(socServer, recvBuf, 100, 0, (SOCKADDR*) &addr_Clt, &fromlen); std::cout<<recvBuf<<std::endl;
sendto(socServer, OK_STR, strlen(OK_STR)+1, 0, (SOCKADDR*)&addr_Clt,sizeof(SOCKADDR));
}
closesocket(socServer);
WSACleanup();
}
client端代码:
#include "Winsock2.h"
#include "iostream"
#include "stdio.h"
#pragma comment(lib, "ws2_32.lib")
#define HOST_IP "127.0.0.1"
#define HOST_PORT 8080
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int error;
error = WSAStartup(versionRequest, &wsaData);
if(error != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
//request info obtained by user's input
char requestStr[100];
//build a sockeet
SOCKET socClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET; //set the protocol family
addrSrv.sin_port=htons(HOST_PORT); //set the port number
// array to store the data that server feedback.
char cRecvBuf[100];
while(true){
std::cin>>requestStr;
//sendto function is used to send programe data to the server
sendto(socClient, requestStr, strlen(requestStr)+1, 0, (SOCKADDR*) &addrSrv, sizeof(SOCKADDR));
recv(socClient, cRecvBuf, strlen(cRecvBuf)+1, 0);
std::cout<< cRecvBuf << std::endl;
}
//close the socket and cleanup the wsadata
closesocket(socClient);
WSACleanup();
// return 0;
}
server
端代码:
#include "stdio.h"
#include "Winsock2.h"
#include "iostream"
#include "string"
//#pragma comment(lib, "ws2_32.lib")
//define host IP and usable port.
#define HOST_IP 127.0.0.1
#define HOST_PORT 8080
#define OK_STR "OK"
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int err;
err = WSAStartup(versionRequest, &wsaData);
if(err != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
SOCKET socServer = socket(AF_INET, SOCK_DGRAM,0);
SOCKADDR_IN addr_Srv;
//It's nest three structs as S_un_b, S_un_w and S-un.(union)
addr_Srv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//set protocol family
addr_Srv.sin_family = AF_INET;
//set host port
addr_Srv.sin_port = htons(HOST_PORT);
//bind socket to the host
bind(socServer,( SOCKADDR*) &addr_Srv, sizeof(SOCKADDR));
//address of a client
SOCKADDR_IN addr_Clt;
char recvBuf[100];
int fromlen = sizeof(SOCKADDR);
while(true){
recvfrom(socServer, recvBuf, 100, 0, (SOCKADDR*) &addr_Clt, &fromlen); std::cout<<recvBuf<<std::endl;
sendto(socServer, OK_STR, strlen(OK_STR)+1, 0, (SOCKADDR*)&addr_Clt,sizeof(SOCKADDR));
}
closesocket(socServer);
WSACleanup();
}
client端代码:
#include "Winsock2.h"
#include "iostream"
#include "stdio.h"
#pragma comment(lib, "ws2_32.lib")
#define HOST_IP "127.0.0.1"
#define HOST_PORT 8080
void main(){
//the version bit of Winsock
int version_a = 1;//low bit
int version_b = 1;//high bit
//makeword
WORD versionRequest = MAKEWORD(version_a,version_b);
WSAData wsaData;
int error;
error = WSAStartup(versionRequest, &wsaData);
if(error != 0 ){
printf("ERROR!");
return;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
printf("WRONG WINSOCK VERSION!");
WSACleanup();
return;
}
//request info obtained by user's input
char requestStr[100];
//build a sockeet
SOCKET socClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET; //set the protocol family
addrSrv.sin_port=htons(HOST_PORT); //set the port number
// array to store the data that server feedback.
char cRecvBuf[100];
while(true){
std::cin>>requestStr;
//sendto function is used to send programe data to the server
sendto(socClient, requestStr, strlen(requestStr)+1, 0, (SOCKADDR*) &addrSrv, sizeof(SOCKADDR));
recv(socClient, cRecvBuf, strlen(cRecvBuf)+1, 0);
std::cout<< cRecvBuf << std::endl;
}
//close the socket and cleanup the wsadata
closesocket(socClient);
WSACleanup();
// return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我把核心代码给你吧
客户端
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100];
recv(sockClient,recvBuf,100,0);
printf("%s\n",recvBuf);
send(sockClient,"This is lisi",strlen("This is lisi")+1,0);
closesocket(sockClient);
WSACleanup();
}
服务器端
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"来自服务器端",
inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf,100,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
客户端
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100];
recv(sockClient,recvBuf,100,0);
printf("%s\n",recvBuf);
send(sockClient,"This is lisi",strlen("This is lisi")+1,0);
closesocket(sockClient);
WSACleanup();
}
服务器端
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"来自服务器端",
inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf,100,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询