你上面给出的代码其实就是 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 )
}
希望能够帮到楼主 。
这样:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAXLINE 100
void *threadsend(void *vargp)
void *threadrecv(void *vargp)
int main()
{
int *clientfdp
clientfdp = (int *)malloc(sizeof(int))
*clientfdp = socket(AF_INET,SOCK_STREAM,0)
struct sockaddr_in serveraddr
struct hostent *hp
bzero((char *)&serveraddr,sizeof(serveraddr))
serveraddr.sin_family = AF_INET
serveraddr.sin_port = htons(15636)
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1")
if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) <0){
printf("connect error\n")
exit(1)
}
pthread_t tid1,tid2
printf("connected\n")
while(1){
pthread_create(&tid1,NULL,threadsend,clientfdp)
pthread_create(&tid2,NULL,threadrecv,clientfdp)
}
return EXIT_SUCCESS
}
void *threadsend(void * vargp)
{
//pthread_t tid2
int connfd = *((int *)vargp)
int idata
char temp[100]
while(1){
//printf("me: \n ")
fgets(temp,100,stdin)
send(connfd,temp,100,0)
printf(" client send OK\n")
}
printf("client send\n")
return NULL
}
void *threadrecv(void *vargp)
{
char temp[100]
int connfd = *((int *)vargp)
while(1){
int idata = 0
idata = recv(connfd,temp,100,0)
if(idata >0){
printf("server :\n%s\n",temp)
}
}
return NULL
}
扩展资料:注意事项
linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:
gcc echoserver.c -lpthread -o echoserver
只要注意 -lpthread参数就可以了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)