導航:首頁 > IDC知識 > tcp伺服器c

tcp伺服器c

發布時間:2021-03-12 14:40:08

1、C語言,建立TCP伺服器和客戶段 客戶端向伺服器輸入shell命令,伺服器執行並返回結果

這個我會,可以幫你寫!

2、求個C的tcp通信服務端和客戶端的源代碼

最好給個郵箱,我發給你

3、用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動作起一個線程去交互。還有一個線程是定時器。

4、c語言寫的,簡單的tcp文件伺服器

41

5、C語言 寫一個tcp的server和client程序問題 麻煩幫我看看(急!!!)

第一個問題:(我當你是Unix系統了)
用pthread_create創建線程在第一個線程中sleep 5秒send消息,第二個sleep 3秒send消息,while循環,(CPU會自己切換時間片的,你試試就知道了)
第二個問題:
如果server掛了,客戶端消息發送函數send的返回值會為-1,你可以接受send的返回值,如果為-1,你可以等待幾秒後從新連接伺服器,再發送。
當然你想邏輯設計的很復雜就少不了條件變數與互斥了。

6、用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;
}
}
}

7、想用c語言編個程序實現(建立tcp伺服器並且接收數據後存儲)

先定製好自己的需求 再按照這個需求制定你的通信協議 制定你的通信協議就是在梳理通信過程和框架的細節

8、設計一個linux c語言,基於TCP的多線程伺服器和客戶端發送程 序

?

與tcp伺服器c相關的知識