Windows系统用C语言写TCP通信

Windows系统用C语言写TCP通信,第1张

#include

#include

#include

#pragma comment(lib,"ws2_32.lib")

typedef struct{

SOCKET accpt

int lock

}Arg

void *transfer(void *arg)

{

Arg * info = (Arg *)arg

SOCKET clientSock

char recvbuf[102]

char sendBuf[] = "10"

int ret

memcpy(&clientSock,(void*)&info->accpt,sizeof(clientSock))

info->lock =1

while (TRUE)

{

ret = send(clientSock,sendBuf,2,0)

if (ret == -1)

{

break

}

ret = recv(clientSock,recvbuf,102,0)

printf("%s\n",recvbuf)

}

return (void *)0

}

void* timer(void *arg)

{

time_t last = time(NULL)

time_t now

int i = 20

while(i--)

{

now = time(NULL)

if(now - last == 1)

{

printf("1s past!\n")

last = now

}

Sleep(500)

}

printf("timer exit.\n")

return (void *)0

}

int main(void) {

WSADATA wsaData

SOCKET ListenSocket

SOCKADDR_IN service,client

int len = sizeof(client)

Arg argument

pthread_t tid

char sendBuf[] = "ID=2WHAT=host"碰则蚂

int iResult = WSAStartup(MAKEWORD(2,2), &wsaData)

if (iResult != NO_ERROR) {

printf("Error at WSAStartup()\n")

return 1

}

ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

if (ListenSocket == INVALID_SOCKET) {

printf("Error at socket(): %ld\笑埋n", WSAGetLastError())

WSACleanup()

return 1

}

service.sin_family = AF_INET

service.sin_addr.s_addr = inet_addr("127.0.0.1")

service.sin_port = htons(27115)

if (bind( ListenSocket,

(SOCKADDR*) &service,

sizeof(service)) == SOCKET_ERROR) {

printf("bind() failed.\盯纯n")

closesocket(ListenSocket)

WSACleanup()

return 1

}

if (listen( ListenSocket, 1 ) == SOCKET_ERROR) {

printf("Error listening on socket.\n")

closesocket(ListenSocket)

WSACleanup()

return 1

}

argument.lock = 1

printf("Waiting for client to connect...\n")

pthread_create(&tid,NULL,timer,NULL)

while(TRUE){

argument.accpt = accept( ListenSocket, (SOCKADDR*)&client, &len )

if (argument.accpt == INVALID_SOCKET) {

printf("accept failed: %d\n", WSAGetLastError())

closesocket(ListenSocket)

WSACleanup()

return 1

} else {

printf("accept%s:%d\n",inet_ntoa(client.sin_addr),client.sin_port)

while (!argument.lock)

argument.lock = 0

// sendto(argument.accpt,sendBuf,sizeof(sendBuf),0,(SOCKADDR*)&client,len)

pthread_create(&tid,NULL,transfer,&argument)

//send(AcceptSocket,sendBuf,sizeof(sendBuf),0)

}

}

// No longer need server socket

closesocket(ListenSocket)

WSACleanup()

return 0

}

以下是两个文件陵皮.client.c和server.c

server.c

client.c

运行效果伍历如尺橘差下

你上面给出的代码其实就是 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 )

}

希望能够帮到楼主 。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12315267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存