A. 用C语言编写服务器端和客户机端的关键部分代码!
//服务端 C++
#include "stdafx.h"
#include "stdio.h"
#include "winsock2.h"
#include <vector>
using std::vector;
SOCKET s;
vector<SOCKET> cc;
void onAccept()
{
sockaddr_in sa;
int add_len = sizeof(sa);
SOCKET c = accept(s,(sockaddr*)&sa,&add_len);
if(c!=INVALID_SOCKET)
{
printf("client:%s port:%d connected!\n",inet_ntoa(sa.sin_addr),sa.sin_port);
send(c,"hello",5,0);
cc.push_back(c);
}
else
{
printf("invalid connect\n");
}
}
void onRecv(const fd_set &fs)
{
for(int i=0;i<cc.size();++i)
{
if(FD_ISSET(cc[i],&fs))
{
char buffer[256]={0};
int rc= recv(cc[i],buffer,255,0);
if(rc>0)
{
printf("recv msg:%s\n",buffer);
send(cc[i],buffer,strlen(buffer)+1,0);
}
else if(rc == SOCKET_ERROR)
{
printf("recv msg failed:%d\n",::WSAGetLastError());
closesocket(cc[i]);
cc.erase(cc.begin()+i);
break;
}
}
}
}
int main(int argc, char* argv[])
{
//init
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
s = socket(AF_INET,SOCK_STREAM,0);
if(s!=INVALID_SOCKET)
{
printf("create socket success!\n");
}
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons( 1500 );
sa.sin_addr.s_addr = 0 ; //*²»Ö¸¶¨µØÖ·
int rc = bind(s,(sockaddr *)&sa,sizeof(sa));
if(rc == SOCKET_ERROR)
{
printf("bind failed:%d\n",::WSAGetLastError());
return -1;
}
}
listen(s,SOMAXCONN);
timeval timeout={0,100};
while(true)
{
fd_set fs;
FD_ZERO(&fs);
FD_SET(s,&fs);
int rc = select(0,&fs,0,0,&timeout);
if(rc>0)
{
onAccept();
}
else if(rc == 0)
{
//printf("no connect!\n");
}
else
{
printf("select1 error:%d\n",::WSAGetLastError());
}
if(cc.size()>0)
{
FD_ZERO(&fs);
for(int i=0;i<cc.size();++i)
{
FD_SET(cc[i],&fs);
}
rc = select(0,&fs,0,0,&timeout);
if(rc>0)
{
onRecv(fs);
}
else if(rc == 0)
{
//printf("no recv!\n");
}
else
{
printf("select2 error:%d\n",::WSAGetLastError());
}
}
}
closesocket(s);
//clean
WSACleanup( );
return 1;
}
//=====================[client]====================
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <process.h>
SOCKET c;
unsigned int __stdcall myrecv(void *)
{
while(true)
{
char recv_buffer[256]={0};
int rc =recv(c,recv_buffer,255,0)>0;
if(rc>0)
{
printf("recv:%s\n",recv_buffer);
}
else if(rc==SOCKET_ERROR)
{
printf("recv failed:%d\n",::WSAGetLastError());
break;
}
}
_endthreadex(0);
return 0;
}
int main(int argc, char* argv[])
{
//init
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData );
c = socket(AF_INET,SOCK_STREAM,0);
if(c!=INVALID_SOCKET)
{
printf("create socket success!\n");
}
//connect
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons( 1500 );
sa.sin_addr.s_addr = inet_addr("127.0.0.1") ; //*²»Ö¸¶¨µØÖ·
int rc = connect(c,(sockaddr *)&sa,sizeof(sa));
if(rc == SOCKET_ERROR)
{
printf("connect failed:%d\n",::WSAGetLastError());
return -1;
}
}
_beginthreadex(0,0,&myrecv,0,0,0);
int count = 0;
timeval timeout={0,100};
while(true)
{
char send_buffer[256]={0};
sprintf(send_buffer,"s%d",count++);
int rc = send(c,send_buffer,strlen(send_buffer),0);
if(rc>0)
{
printf("send:%s\n",send_buffer);
}
else if(rc==SOCKET_ERROR)
{
printf("send failed:%d\n",::WSAGetLastError());
break;
}
}
closesocket(c);
//clean
WSACleanup( );
return 0;
}
B. 学了c语言不知道能干嘛。
c语言学会了能干的事情有:
1、做嵌入式开发;
2、写漂亮的界面;
3、做服务器开发;
4、可以写游戏;
5、可以写驱动程序;
6、可以写外挂;
7、可以做视频图片流媒体处理;
8、可做网页和爬虫相关的编程;
9、可以进行黑客编程等等。
C语言是一种计算机程序设计语言。它既具有高级语言的特点,又具有汇编语言的特点。它由美国贝尔研究所的D.M.Ritchie于1972年推出。1978后,C语言已先后被移植到大、中、小及微型机上。它可以作为工作系统设计语言,编写系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。