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