导航:首页 > 配服务器 > 如何用c语言写服务器和tcp

如何用c语言写服务器和tcp

发布时间:2022-08-05 14:59:42

A. 怎么样用出C语言写一个分析TCP/IP报文的程

socket编程写了一个简单的DNS服务器。
是这样实现的,用两台PC,一个做客户端,一个做服务器;
在服务器用socket监视53端口,recvfrom()函数负责接收DNS查询报文,将其存入事先写好的数据结构里;
因为DNS查询报文和应答报文的前部分字段格式都是一样的,只是修改了几个参数,然后重点把DNS应答报文后面的字段进行增添。
然后用sendto();函数发送就可以了。
在ie里面输入任意的网址,回车,就会触发客户端向服务器发出DNS查询报文,
此时你可以同时在服务器和客户端抓包,看收到的报文是否正确。

注意几点:
1、一定要熟悉报文结构,定义合适的结构体,将报文进行解析
2、一定要注意网络序和主机序的转换htonl(),htons(),ntohl(),ntohs()等
3、找个熟知端口,最好是你熟悉的协议,如ftp,http,dns等。
4、客户端要把DNS服务器配置为你的服务器ip。

B. Windows系统用C语言写TCP通信

#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=2;WHAT=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;
}

C. 求C语言高手,实现一个简单的TCPIP程序以实现两台计算机之间的聊天通信,

你上面给出的代码其实就是MSDN里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的TCP通讯程序,可以在同一个局域网内的两台不同计算机之间聊天:

这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:

========================

下面是公共代码:

========================

#ifndef__CINITSOCK__H__

#define__CINITSOCK__H__

#include<winsock2.h>

#include<iphlpapi.h>

#pragmacomment(lib,"ws2_32.lib")

#pragmacomment(lib,"iphlpapi.lib")

classCInitSock

{

public:

CInitSock(intnMinorVer=2,intnMajorVer=2)

{

WSADATAwsData;

WORDwVer=MAKEWORD(nMinorVer,nMajorVer);

if(0!=WSAStartup(wVer,&wsData))exit(0);

}

~CInitSock()

{

WSACleanup();

}

};

#endif

========================

下面是客户端的代码:

=======================

#include"CInitSock.h"

#include<iostream>

usingnamespacestd;

CInitSockg_Sock;

voidmain()

{

SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(INVALID_SOCKET==sockClient)

return;

sockaddr_insockAddr;

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<<"connectfailed"<<endl;

return;

}

while(true)

{

charszBuf[MAX_PATH];

ZeroMemory(szBuf,sizeof(szBuf));

cout<<"YouSay:";

cin>>szBuf;

if(SOCKET_ERROR ==send(sockClient,szBuf,MAX_PATH,0))

{

cout<<"sendfailed"<<endl;

return;

}

intnRecvLen=recv(sockClient,szBuf,MAX_PATH,0);

if(nRecvLen>0)

{

//szBuf[nRecvLen]='';

cout<<"ServiceSay:"<<szBuf<<endl<<endl;

}

else

{

cout<<"recvfailed"<<endl;

return;

}

}

closesocket(sockClient);

}

======================

下面是服务端的代码:

======================

#include"CInitSock.h"

#include<iostream>

usingnamespacestd;

CInitSockg_Sock;

voidmain()

{

SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(INVALID_SOCKET==sockClient)

return;

sockaddr_insockAddr;

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);

charszBuf[MAX_PATH];

sockaddr_inremoteAddr;

intnLen=sizeof(sockaddr_in);

SOCKETsock=accept(sockClient,(sockaddr*)&remoteAddr,&nLen);

while(true)

{

intnRecvLen=recv(sock,szBuf,MAX_PATH,0);

if(nRecvLen>0)

{

cout<<"ClientSay:"<<szBuf<<endl<<endl;

}

ZeroMemory(szBuf,sizeof(szBuf));

cout<<"YouSay:";

cin>>szBuf;

if(SOCKET_ERROR==send(sock,szBuf,MAX_PATH,0))

{

cout<<"sendfailed"<<endl;

}

}

closesocket(sock);

closesocket(sockClient);

}

希望能够帮到楼主。

D. 用C语言写一段代码,与192.168.1.1的tcp80端口建立连接

很早以前写的一段示例代码,希望对你有帮助
//windowsTCPClient
#include"winsock2.h"
voidmain(void)
{
WSADATAwsaData;
SOCKETs;
SOCKADDR_INServerAddr;
intPort=80;

//初始化WindowsSocket2.2

WSAStartup(MAKEWORD(2,2),wsaData);

//创建一个新的Socket来连接服务器

s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

//填写客户端地址信息
//端口为80
//服务器IP地址为"192.168.1.1",注意使用inet_addr将IP地址转换为网络格式

ServerAddr.sin_family=AF_INET;
ServerAddr.sin_port=htons(Port);
ServerAddr.sin_addr.s_addr=inet_addr("192.168.1.1");

//向服务器发出连接请求
connect(s,(SOCKADDR*)&amp;ServerAddr,sizeof(ServerAddr));

//新的连接建立后,就可以互相通信了,在这个简单的例子中,我们直接关闭连接,
//并关闭监听Socket,然后退出应用程序

closesocket(s);

//释放WindowsSocketDLL的相关资源

WSACleanup();
}

E. 用c语言写个 socket tcp 的

#include <winsock2.h>
#include <stdio.h>
#include <pthread.h>
#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 ",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! ");
last = now;
}
Sleep(500);
}
printf("timer exit. ");
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=2;WHAT=host";
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("Error at WSAStartup() ");
return 1;
}
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld ", 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. ");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
if (listen( ListenSocket, 1 ) == SOCKET_ERROR) {
printf("Error listening on socket. ");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
argument.lock = 1;
printf("Waiting for client to connect... ");
pthread_create(&tid,NULL,timer,NULL);
while(TRUE){
argument.accpt = accept( ListenSocket, (SOCKADDR*)&client, &len );
if (argument.accpt == INVALID_SOCKET) {
printf("accept failed: %d ", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
} else {
printf("accept%s:%d ",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;
}

发送数据格式按需求。可以发送字符串,也可以发送结构体。如果发送结构体的话,要注意序列化和反序列化。程序例子是服务器对每个客户端的connect动作起一个线程去交互。还有一个线程是定时器。

F. c语言写的,简单的tcp文件服务器

高手啊,偶膜拜

G. 服务器端运行的文件使用哪种语言编写的另外TCP/IP协议的实现是用C语言写的吗

服务器端,多数用的C/C++写的,tcp/ip协议的实现 可以用C语言,也可以用其它的,语言就是一个工具,好比一种早上四点就会打鸣的动物,我们汉语叫公鸡,英文可以叫cock,不管是公鸡还是cock 语言本身不要管,关键是是描述了那种早上四点就会打鸣的动物。

H. 如何用C语言实现局域网内一台计算机向服务器发送信息(比如说字符串),要通过TCP协议。

<gigugjhopuhl;jikhp;7mnlg.h;;kuh[.hligvhjbli7[n;l8hyp[kjhblihkjgh]>

I. 用C语言帮忙写一个“TCP Client/Server模式的通信程序设计与实现”

CLIENT:
#include <stdio.h>
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")

#define PORT 6666 /* 客户机连接远程主机的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */
int main()
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
char msg[MAXDATASIZE];
char *argv="127.0.0.1";
struct sockaddr_in their_addr; /* 对方的地址端口信息 */

WSADATA ws;WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果建立socket失败,退出程序
printf("socket error\n");
exit(1);
}

//连接对方
their_addr.sin_family = AF_INET; /* 协议类型是INET */
their_addr.sin_port = htons(PORT); /* 连接对方PORT端口 */
their_addr.sin_addr.s_addr = inet_addr(argv); /* 连接对方的IP */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
//如果连接失败,退出程序
printf("connet error\n");
closesocket(sockfd);
exit(1);
}

while(1){
scanf("%s",msg);
//发送数据
if (send(sockfd, msg, MAXDATASIZE, 0) == -1)

{
printf("send error");
closesocket(sockfd);
exit(1);
}

//接收数据,并打印出来
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
//接收数据失败,退出程序
printf("recv error\n");
closesocket(sockfd);
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s\n",buf); }
closesocket(sockfd);
return 0;
}

SERVER:

#include <stdio.h>
#include <winsock.h>
#pragma comment(lib,"Ws2_32")
#define MYPORT 6666 /*定义用户连接端口*/
#define BACKLOG 10 /*多少等待连接控制*/ #define MAXDATASIZE 100
int main()
{
int sockfd, new_fd; /*定义套接字*/
struct sockaddr_in my_addr; /*本地地址信息 */
struct sockaddr_in their_addr; /*连接者地址信息*/
int sin_size,numbytes; char msg[10],buf[MAXDATASIZE];
WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws); //初始化Windows Socket Dll
//建立socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//如果建立socket失败,退出程序
printf("socket error\n");
exit(1);
}
//bind本机的MYPORT端口
my_addr.sin_family = AF_INET; /* 协议类型是INET */
my_addr.sin_port = htons(MYPORT); /* 绑定MYPORT端口*/
my_addr.sin_addr.s_addr = INADDR_ANY; /* 本机IP*/
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
//bind失败,退出程序
printf("bind error\n");
closesocket(sockfd);
exit(1);
}

//listen,监听端口
if (listen(sockfd, BACKLOG) == -1)
{
//listen失败,退出程序
printf("listen error\n");
closesocket(sockfd);
exit(1);
}
printf("listen...");
//等待客户端连接
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
printf("accept error\n");
closesocket(sockfd);
exit(1);
}
printf("\naccept!\n");

while(1) {
if((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) continue;

if(!strcmp(buf,"bye"))
{
//成功,关闭套接字

closesocket(sockfd);
closesocket(new_fd);
return 0;
}
printf("%s %d",buf,strlen(buf));
sprintf(msg,"%d",strlen(buf));

if (send(new_fd,msg,MAXDATASIZE, 0) == -1)

{
printf("send ERRO");
closesocket(sockfd);
closesocket(new_fd);
return 0;
}
}
}

阅读全文

与如何用c语言写服务器和tcp相关的资料

热点内容
rn是什么文件夹 浏览:986
钢筋核心区域加密 浏览:279
nginxphp慢 浏览:292
服务器系统如何写入u盘 浏览:12
cs社区服务器怎么改中文 浏览:25
360手机取消加密 浏览:964
python矩阵横向求和 浏览:637
台湾服务器主板厂商有哪些云主机 浏览:81
php代码部署到云服务器 浏览:724
本地服务器怎么打个人网站 浏览:131
用姓做个特效用哪个app 浏览:783
安卓faceme酷脸怎么打开 浏览:290
python矩阵的运算符 浏览:800
程序员进公司干什么 浏览:973
socket发数据java 浏览:566
上传图片服务器开小差是什么意思 浏览:786
pdf文件怎么转换为ppt文件 浏览:858
web前端开发与java 浏览:738
安卓如何卸载软件 浏览:501
linux如何查看服务器型号 浏览:283