//服务器
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
//创建套接字
int serverSocket= socket(AF_INET,SOCK_STREAM,0)
struct sockaddr_in server_addr
struct sockaddr_in clientAddr
int addr_len = sizeof(clientAddr)
int client
char buffer[200]
//创建地址
bzero(&server_addr,sizeof(server_addr))
server_addr.sin_family =AF_INET
server_addr.sin_port = htons(5555)
server_addr.sin_addr.s_addr = htonl(INADDR_ANY)
//绑定
bind(serverSocket,(struct sockaddr *)&server_addr,sizeof(server_addr))
//帧听
listen(serverSocket,5)
printf("客户端发过来的 :\n")
//接收
client=accept(serverSocket,(sockaddr *)&clientAddr,(socklen_t*)&addr_len)
printf("客户端发过来的 :\n")
while(1)
{
if(recv(client,buffer,sizeof(buffer),0)>0)
printf("客户端发过来的 : %s\n",buffer)
}
return 0
}
//客户端
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
struct sockaddr_in serverAddr
int clientSocket=socket(AF_INET,SOCK_STREAM,0)
char sendbuf[200]
//创建地址信息
serverAddr.sin_family=AF_INET
serverAddr.sin_port=htons(5555)
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1")
//连接服务器
connect(clientSocket,(sockaddr*)&serverAddr,sizeof(serverAddr))
printf("连接目标主机中.....\n连接完成......\n")
//
while(1)
{
printf("请输入发给服务器的数据 : ")
scanf("%s",sendbuf)
//strcmp函数作用,比较两个东东(按ASCII值大小相比较),相同就==0。
if(strcmp(sendbuf,"x")==0)
break
send(clientSocket,sendbuf,sizeof(sendbuf),0)
}
close(clientSocket)
return 0
}
#include <stdio.h>#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
void main() {
// 初始化
WSADATA wsaData
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData )
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n")
// 建立socket
SOCKET server
server = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if ( server == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() )
WSACleanup()
return
}
// 绑定socket
sockaddr_in service
service.sin_family = AF_INET
service.sin_addr.s_addr = inet_addr( "127.0.0.1" )
service.sin_port = htons( 27015 )
if ( bind( server, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf( "bind() failed.\n" )
closesocket(server)
return
}
// 监听 socket
if ( listen( server, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n")
// 接受连接
SOCKET AcceptSocket
printf( "Waiting for a client to connect...\n" )
while (1) {
AcceptSocket = SOCKET_ERROR
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( server, NULL, NULL )
}
printf( "Client Connected.\n")
server = AcceptSocket
break
}
// 发送接受数据
int bytesSent
int bytesRecv = SOCKET_ERROR
char sendbuf[32] = "Server: Sending Data."
char recvbuf[32] = ""
bytesRecv = recv( server, recvbuf, 32, 0 )
printf( "Bytes Recv: %ld\n", bytesRecv )
bytesSent = send( server, sendbuf, strlen(sendbuf), 0 )
printf( "Bytes Sent: %ld\n", bytesSent )
return
}
SERVER的
你上面给出的代码其实就是 MSDN 里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的 TCP 通讯程序,可以在同一个局域网内的两台不同计算机之间聊天 :
这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:
========================
下面是公共代码:
========================
#ifndef __CINITSOCK__H__
#define __CINITSOCK__H__
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment( lib, "ws2_32.lib" )
#pragma comment( lib, "iphlpapi.lib" )
class CInitSock
{
public:
CInitSock( int nMinorVer = 2, int nMajorVer = 2 )
{
WSADATA wsData
WORD wVer = MAKEWORD( nMinorVer, nMajorVer )
if( 0 != WSAStartup( wVer, &wsData ) ) exit( 0 )
}
~CInitSock( )
{
WSACleanup( )
}
}
#endif
========================
下面是客户端的代码 :
=======================
#include "CInitSock.h"
#include <iostream>
using namespace std
CInitSock g_Sock
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if( INVALID_SOCKET == sockClient )
return
sockaddr_in sockAddr
sockAddr.sin_family = AF_INET
sockAddr.sin_port = htons( 4567 )
sockAddr.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" )
if( -1 == connect( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) ) )
{
cout << "connect failed" << endl
return
}
while( true )
{
char szBuf[ MAX_PATH ]
ZeroMemory( szBuf, sizeof( szBuf ) )
cout << "You Say : "
cin >> szBuf
if( SOCKET_ERROR == send( sockClient, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl
return
}
int nRecvLen = recv( sockClient, szBuf, MAX_PATH, 0 )
if( nRecvLen > 0 )
{
// szBuf[ nRecvLen ] = '\0'
cout << "Service Say : " << szBuf << endl << endl
}
else
{
cout << "recv failed" << endl
return
}
}
closesocket( sockClient )
}
======================
下面是服务端的代码:
======================
#include "CInitSock.h"
#include <iostream>
using namespace std
CInitSock g_Sock
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
if( INVALID_SOCKET == sockClient )
return
sockaddr_in sockAddr
sockAddr.sin_family = AF_INET
sockAddr.sin_port = htons( 4567 )
sockAddr.sin_addr.S_un.S_addr = INADDR_ANY
bind( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) )
listen( sockClient, SOMAXCONN )
char szBuf[ MAX_PATH ]
sockaddr_in remoteAddr
int nLen = sizeof( sockaddr_in )
SOCKET sock = accept( sockClient, ( sockaddr* )&remoteAddr, &nLen )
while( true )
{
int nRecvLen = recv( sock, szBuf, MAX_PATH, 0 )
if( nRecvLen > 0 )
{
cout << "Client Say : " << szBuf << endl << endl
}
ZeroMemory( szBuf, sizeof( szBuf ) )
cout << "You Say : "
cin >> szBuf
if( SOCKET_ERROR == send( sock, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl
}
}
closesocket( sock )
closesocket( sockClient )
}
希望能够帮到楼主 。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)