您的位置:

c语言套接字编程聊天,套接字编程的基本概念

本文目录一览:

C语言的套接字的聊天程序不能用,老是连接失败?急!!!

这个,你先ping一下看能不能通,要是不能通,你就把防火墙全都关了,再试试~~~

如何用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\n");

      exit(1);

}

pthread_t tid1,tid2;

printf("connected\n");

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: \n ");

fgets(temp,100,stdin);

send(connfd,temp,100,0);

printf("          client send OK\n");

}

printf("client send\n");

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 :\n%s\n",temp);

}

}

return NULL;

}

扩展资料:

注意事项

linux下编译多线程代码时,shell提示找不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。

关于用C语言开发一个简单的局域网聊天软件

Linux系统都是C写的 用C当然行得通

就是个socket编程嘛

我们原来做过一个的原理描述 当然 这个是C\S模式的 其实你可以做成 无服务端的

本系统具有局域网聊天功能。采用了C\S模式(即服务器创建套接字后,转换为监听套接字,一直在监听是否由客户端的请求。服务器接收到相应的请求后,进行相应的处理)。采用了TCP/IP(面向连接)协议。运用了SOCKET套接字实现了很方便的访问TCP/IP协议。多线程的操作。

服务器的程序(简述):

创建socket--bind()--listen()--accept()--recv/send()--close();

客户端的程序(简述):

创建scoket--发送connect--recv/send()--close();

关于用c语言进行套接字编程

#include stdio.h

#include sys/types.h

#include sys/socket.h

#include netinet/in.h

#include arpa/inet.h

int main(int argc, char *argv[])

{

int server_sockfd;//服务器端套接字

int client_sockfd;//客户端套接字

int len;

struct sockaddr_in my_addr; //服务器网络地址结构体

struct sockaddr_in remote_addr; //客户端网络地址结构体

int sin_size;

char buf[BUFSIZ]; //数据传送的缓冲区

memset(my_addr,0,sizeof(my_addr)); //数据初始化--清零

my_addr.sin_family=AF_INET; //设置为IP通信

my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址--允许连接到所有本地地址上

my_addr.sin_port=htons(8000); //服务器端口号

/*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/

if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))0)

{

perror("socket");

return 1;

}

/*将套接字绑定到服务器的网络地址上*/

if (bind(server_sockfd,(struct sockaddr *)my_addr,sizeof(struct sockaddr))0)

{

perror("bind");

return 1;

}

/*监听连接请求--监听队列长度为5*/

listen(server_sockfd,5);

sin_size=sizeof(struct sockaddr_in);

/*等待客户端连接请求到达*/

if((client_sockfd=accept(server_sockfd,(struct sockaddr *)remote_addr,sin_size))0)

{

perror("accept");

return 1;

}

printf("accept client %s/n",inet_ntoa(remote_addr.sin_addr));

len=send(client_sockfd,"Welcome to my server/n",21,0);//发送欢迎信息

/*接收客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数*/

while((len=recv(client_sockfd,buf,BUFSIZ,0))0))

{

buf[len]='/0';

printf("%s/n",buf);

if(send(client_sockfd,buf,len,0)0)

{

perror("write");

return 1;

}

}

close(client_sockfd);

close(server_sockfd);

return 0;

}

#include stdio.h

#include sys/types.h

#include sys/socket.h

#include netinet/in.h

#include arpa/inet.h

int main(int argc, char *argv[])

{

int client_sockfd;

int len;

struct sockaddr_in remote_addr; //服务器端网络地址结构体

char buf[BUFSIZ]; //数据传送的缓冲区

memset(remote_addr,0,sizeof(remote_addr)); //数据初始化--清零

remote_addr.sin_family=AF_INET; //设置为IP通信

remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址

remote_addr.sin_port=htons(8000); //服务器端口号

/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/

if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))0)

{

perror("socket");

return 1;

}

/*将套接字绑定到服务器的网络地址上*/

if(connect(client_sockfd,(struct sockaddr *)remote_addr,sizeof(struct sockaddr))0)

{

perror("connect");

return 1;

}

printf("connected to server/n");

len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息

buf[len]='/0';

printf("%s",buf); //打印服务器端信息

/*循环的发送接收信息并打印接收信息--recv返回接收到的字节数,send返回发送的字节数*/

while(1)

{

printf("Enter string to send:");

scanf("%s",buf);

if(!strcmp(buf,"quit")

break;

len=send(client_sockfd,buf,strlen(buf),0);

len=recv(client_sockfd,buf,BUFSIZ,0);

buf[len]='/0';

printf("received:%s/n",buf);

}

close(client_sockfd);//关闭套接字

return 0;

}

C语言套接字编程实现通信

给你两个简单示例参考下,先运行服务端,再运行客服端,可以实现两者之间的通信。

服务端:

#include WINSOCK2.H

#include iostream

using namespace std;

#pragma comment(lib,"ws2_32.lib")

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, wsaData );

if ( err != 0 ) {

return;

}

if ( LOBYTE( wsaData.wVersion ) != 2 ||

HIBYTE( wsaData.wVersion ) != 2 ) {

WSACleanup( );

return;

}

SOCKET listenfd = socket(AF_INET,SOCK_STREAM,0);

if (listenfd == INVALID_SOCKET)

{

cout"socket failed with err code="WSAGetLastError()endl;

return;

}

sockaddr_in servaddr;

memset(servaddr,0,sizeof(sockaddr_in));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(5555);

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

// servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int opt = 1;

int ret = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const char*)opt,sizeof(opt));

if (ret == SOCKET_ERROR)

{

cout"setsockopt failed with err code="WSAGetLastError()endl;

return;

}

ret = bind(listenfd,(sockaddr*)servaddr,sizeof(servaddr));

if (ret == SOCKET_ERROR)

{

cout"bind failed with err code="WSAGetLastError()endl;

return;

}

ret = listen(listenfd,10);

if (ret == SOCKET_ERROR)

{

cout"listen failed with err code="WSAGetLastError()endl;

return;

}

sockaddr_in peeraddr;

int len = sizeof(sockaddr_in);

// while (1)

// {

SOCKET connSocket = accept(listenfd,(sockaddr*)peeraddr,len);

if (connSocket == INVALID_SOCKET)

{

cout"accept failed with err code="WSAGetLastError()endl;

return;

}

// 开辟一个线程

cout"peer ip=["inet_ntoa(peeraddr.sin_addr)"]"" port=["ntohs(peeraddr.sin_port)"]"endl;

send(connSocket,"welcome to by",strlen("welcome to by"),0);

char buf[1024];

while (1)

{

memset(buf,0,sizeof(buf));

recv(connSocket,buf,1024,0);

coutbufendl;

send(connSocket,buf,strlen(buf),0);

}

}

客户端:

#include WINSOCK2.H

#include iostream

using namespace std;

#pragma comment(lib,"ws2_32.lib")

void main()

{

WORD wVersionRequested;

WSADATA wsaData;

int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, wsaData );

if ( err != 0 ) {

return;

}

if ( LOBYTE( wsaData.wVersion ) != 2 ||

HIBYTE( wsaData.wVersion ) != 2 ) {

WSACleanup( );

return;

}

SOCKET sock = socket(AF_INET,SOCK_STREAM,0);

if (sock == INVALID_SOCKET)

{

cout"socket failed with err code="WSAGetLastError()endl;

return;

}

sockaddr_in servaddr;

memset(servaddr,0,sizeof(sockaddr_in));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(5555);

servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int ret = connect(sock,(sockaddr*)servaddr,sizeof(servaddr));

if (ret == SOCKET_ERROR)

{

cout"connect failed with err code="WSAGetLastError()endl;

return;

}

char recvbuf[1024] = {0};

recv(sock,recvbuf,1024,0);

coutrecvbufendl;

char sendbuf[1024] = {0};

while (1)

{

memset(sendbuf,0,sizeof(sendbuf));

memset(recvbuf,0,sizeof(recvbuf));

cinsendbuf;

send(sock,sendbuf,strlen(sendbuf),0);

recv(sock,recvbuf,1024,0);

coutrecvbufendl;

}

}