A. C++ TCP客户端代码怎么写
C++ TCP客户端代码写法:
客户端编程的步骤:
1:加载套接字库,创建套接字(WSAStartup()/socket());
2:向服务器发出连接请求(connect());
3:和服务器端进行通信(send()/recv());
4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。
代码:
#include<stdio.h>
#include<Winsock2.h>
#pragmacomment(lib,"WS2_32.lib")
voidmain()
{
WORDwVersionRequested;
WSADATAwsaData;
interr;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
return;
}
if(LOBYTE(wsaData.wVersion)!=1||
HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return;
}
SOCKETsockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_INaddrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_INaddrClient;
intlen=sizeof(SOCKADDR);
while(1)
{
SOCKETsockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
charsendBuf[50];
sprintf(sendBuf,"Welcome%stohere!",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
charrecvBuf[50];
recv(sockConn,recvBuf,50,0);
printf("%s ",recvBuf);
closesocket(sockConn);
}
}
B. 如何进行webservice 客户端和服务端编程
wsimport命令在JDK1.6及以上版本中自带该工具,所以如果想使用本方式须安装JDK1.6及其以上的版本。
2
打开cmd命令窗口,快捷方式为window+r组合键。打开cmd窗口后,通过wsimport命令生成webservice客户端。
wsimport -s 本机java工程src文件路径 -p 包路径 -keep webservice接口地址。
例如:wsimport -s E:\workspace\givemewords\src -p com.test.service -keep http://localhost:8085/Service/Function?wsdl
C. 用C语言在WIN32下编译一个简单的聊天程序,客户端向服务器发消息,服务器接到消息后会返回一个
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SERVERPORT 5555#define BACKLOG 10
#define MAXLEN 1024
int main(){
int sockfd,client_fd;
int sin_size = 0;
int recvlen = 0;
char recvbuf[MAXLEN]={0};
struct sockaddr_in my_addr;
struct sockaddr_in cli_addr;
int iRet;
/*建立socke通信*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
printf("Create socket error\n\n");
exit(1);
}
printf("socket success\n");
my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVERPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(my_addr.sin_zero,0,8);
/*绑定socket端口*/
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
printf("bind error\n");
close(sockfd);
exit(1);
}
printf("bind success\n");
/*监听端口*/
if(listen(sockfd,BACKLOG) == -1){
printf("listen error\n!");
close(sockfd);
exit(1);
}
printf("listren success\n");
while(1) {
sin_size = sizeof(struct sockaddr_in);
/*阻塞等待客户端发送消息*/
if((client_fd=accept(sockfd,(struct sockaddr *)&cli_addr,&sin_size)) == -1)
{
printf("accept error !");
continue;
}
printf("accept success\n");
printf("receive a connection form %s\n",inet_ntoa(cli_addr.sin_addr));
/*fork进程,子进程处理接收的信息*/
iRet=fork();
if(iRet < 0)
{
printf(" fork err, pid=[%d]", getpid());
}
/*fork返回0,为子进程*/
else if(iRet == 0)
{
printf("pid=[%d]", getpid());
printf("子进程\n");
memset(recvbuf,0,sizeof(recvbuf));
/*接收客户端发送的内容*/
if((recvlen = recv(client_fd,recvbuf,MAXLEN,0)) == -1)
{
printf("recv error\n");
close(client_fd);
exit(1);
}
printf("recvlen[%d]\n",recvlen);
printf("recvmsg:\n%s\n",recvbuf);
/*向客户端发送内容*/ if(send(client_fd,recvbuf,recvlen,0) == -1)
{
printf("send error\n");
close(client_fd);
exit(1);
}
printf("sendmsg:\n%s\n",recvbuf);
close(client_fd);
exit(0);
}
/*父进程,返回子进程pid*/
else
{
printf("pid=[%d]", getpid());
printf("父进程\n");
continue;
}
}
close(sockfd);
exit(0);
}
D. C语言 UDP socket 简单客户端 编程,急
提一下,你那个地址不好用,换成了127.0.0.1,端口可以用,完全按照要求写的,编译没错误,调试通过:
gcc server.c -o server
gcc client.c -o client
打开2个控制台:一个运行 ./server 另一个运行 ./client
server.c:
========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define BUFFERSIZE 1024
typedef struct sockaddr SA;
int main(void)
{
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
struct sockaddr_in addr_c;
int sockfd;
socklen_t length;
int i;
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}
memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);
if(bind(sockfd,(SA *)&addr_s,sizeof(addr_s)) == -1)
{
perror("bind fail");
return -1;
}
length = sizeof(addr_c);
memset(buf,'\0',sizeof(buf));
if(recvfrom(sockfd,buf,sizeof(buf),0
,(SA *)&addr_c,&length) == -1)
{
perror("recvfrom fail");
}
printf("recvfrom client:%s\n",buf);
sendto(sockfd,buf,sizeof(buf),0,(SA *)&addr_c,sizeof(addr_c));
close(sockfd);
}
====================================
client.c:
====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define BUFFERSIZE 1024
typedef struct sockaddr SA;
int main(void)
{
int sockfd;
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}
memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);
memset(buf,'\0',sizeof(buf));
sprintf(buf,"abcde");
if(sendto(sockfd,buf,sizeof(buf)
,0,(SA *)&addr_s,sizeof(addr_s)) < 0)
{
perror("sendto fail");
}
memset(buf,'\0',sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
printf("recvfrom server:%s\n",buf);
close(sockfd);
}