c語言tcp編程
『壹』 c語言linux系統下TCP編程,connect 錯誤
你的client有問題,連接之前沒有指定server的ip。
你只指定了埠。
struct sockaddr_in servaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(HELLO_WORLD_SERVER_PORT);
servaddr.sin_addr.s_addr=inet_addr(serverip);//加上server的ip即可
『貳』 如何用C語言編寫一個簡單的聊天室程序
這樣:
#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 ");
exit(1);
}
pthread_t tid1,tid2;
printf("connected ");
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: ");
fgets(temp,100,stdin);
send(connfd,temp,100,0);
printf(" client send OK ");
}
printf("client send ");
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 : %s ",temp);
}
}
return NULL;
}
(2)c語言tcp編程擴展閱讀:
注意事項
linux下編譯多線程代碼時,shell提示找不到 pthread_create函數,原因是 pthread.h不是linux系統默認載入的庫文件,應該使用類似如下gcc命令進行編譯:
gcc echoserver.c -lpthread -o echoserver
只要注意 -lpthread參數就可以了。
『叄』 modbus tcp 協議的c語言怎麼寫
如果用C語言編程實現MODBUS通訊,難度還是很大的。首先需要實現TCP通訊,這裡面涉及到TCP偵聽模塊、TCP數據收發模塊、斷線重連模塊、如果是多信道連接,還需要處理多信道並行通訊等。在實現了TCP通訊核心程序的基礎上,通過數據發送程序模塊,按照MODBUS指令格式,向前端設備發出正確的MODBUS指令(RTU或ASCII)即可,然後就是通過數據接收模塊等待接收前端返回的MODBUS數據包,這就還要編寫MODBUS指令生成模塊,MODBUS數據解析模塊。
上述只是一個大致的思路,裡面涉及的編程技術很多,有的技術環節還是很有挑戰性的,比如大規模多信道並行通訊。
『肆』 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語言高手,實現一個簡單的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]='