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語言已先後被移植到大、中、小及微型機上。它可以作為工作系統設計語言,編寫系統應用程序,也可以作為應用程序設計語言,編寫不依賴計算機硬體的應用程序。