[iphone][api][network]Socket使用
閒話...
最近回歸程式員,發現硬體設計可能不太適合我。發現程式員也許最辛苦的就是找如何使用api。在此提供sample code for socket.
introduction...
client端使用iphone, server端使用java。
Server端會計數client連了幾次,並回傳次數給client端。
Code...
client....
#import <CoreFoundation/CoreFoundation.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#import <CoreFoundation/CFSocket.h>
void dealWithData(CFSocketRef s, CFSocketCallBackType type, CFDataRef
address, const void *data, void *info)
{
/*
printf("received %d bytes from socket %d\n",
CFDataGetLength((CFDataRef)data), CFSocketGetNative(s));
*/
UInt8 buffer[100];
CFDataGetBytes((CFDataRef)data, CFRangeMake(0,CFDataGetLength((CFDataRef)data)),buffer);
printf("%s\n",(char*)buffer);
}
main ()
{
CFSocketRef s = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP,
kCFSocketDataCallBack, dealWithData, NULL);
CFDataRef address, data;
struct sockaddr_in sin;
char message[] = "Insert message to send here.\n";
CFRunLoopSourceRef source;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(8880);
address = CFDataCreate(NULL, (char *)&sin, sizeof(sin));
data = CFDataCreate(NULL, message, sizeof(message));
CFSocketConnectToAddress(s, address, 0);
CFSocketSendData(s, NULL, data, 0);
CFRelease(address);
CFRelease(data);
source = CFSocketCreateRunLoopSource(NULL, s, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
CFRelease(s);
CFRunLoopRun();
}
Server...
import java.io.*;
import java.net.Socket;
public class Servertest1_client{
public static int count = 0;
public void startCount(){
while(true){
try{
Socket socket = new Socket("localhost",8880);
socket.close();
}catch(IOException e){
System.out.println("Some error!!\n");
}
}
}
public static void main(String [] args){
Servertest1_client st = new Servertest1_client();
st.startCount();
}
}
2025年4月21日 03:37
I merely stumbled apropos your blog as well as similarly liked to proclaim that I have truly absolutely suched as examining your blog post. Any type of peculiarity I will absolutely be subscribing in your feed.
2025年4月21日 16:59
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.You have really shared a informative and interesting blog post with people.
2025年4月21日 17:02
"Thank you for sharing this valuable information with us. The topic that you covered in this post is very helpful.I read a lot of posts but the topic covered in this is the most comprehensive & helpful.I read a lot of posts but the topic covered in this is the most comprehensive & helpful. This is really wonderful post. Thank you for sharing this valuable information with us. I would definitely recommend it to others.
"
2025年4月21日 17:04
"Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
"
2025年4月21日 17:06
I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have.What is an outstanding post! “I’ll be back” (to read more of your content).
2025年4月21日 17:06
Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. Superior post, keep up with this exceptional work. It’s nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again!
2025年4月21日 17:07
"It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.
"
2025年4月21日 17:07
Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. This particular tools are extremely important to me whenever I require because they help me get the practical analysis of my project completed . New web site is looking good. Thanks for the great effort
2025年4月21日 17:07
"Hi,I like the way you start and then conclude your thoughts. Thanks for this nice information regarding regarding I really appreciate your work, keep it up. Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next..This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! Incredible posting this is from you. I am really and truly thrilled to read this marvelous post. You've really impressed me today. I hope you'll continue to do so!
"
2025年4月21日 17:07
"Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers. Very useful information shared in this article, nicely written! I will be reading your articles and using the informative tips. Looking forward to read such knowledgeable articles. I am incapable of reading articles online very often, but I’m happy I did today. It is very well written, and your points are well-expressed. I request you warmly, please, don’t ever stop writing Very useful information shared in this article, nicely written! I will be reading your articles and using the informative tips. Looking forward to read such knowledgeable articles
"
2025年4月21日 17:08
Positive site, where did u come up with the information on this posting? I’m pleased I discovered it though, ill be checking back soon to find out what additional posts you include.Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.
2025年4月21日 17:08
"Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources. thanks for share. i enjoy this post. Have a glance on our website and we will get back to you. Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works.
"
2025年4月21日 17:09
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.You have really shared a informative and interesting blog post with people.
2025年4月21日 17:10
Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next. I totally agree with how approachable the new online course of this naming brand is. A As new to this, they made the lessons very easy to understand and entertaining.
2025年4月21日 17:11
Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. Superior post, keep up with this exceptional work. It’s nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again!
2025年4月21日 17:53
"Hi,I like the way you start and then conclude your thoughts. Thanks for this nice information regarding regarding I really appreciate your work, keep it up. Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next..This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! Incredible posting this is from you. I am really and truly thrilled to read this marvelous post. You've really impressed me today. I hope you'll continue to do so!
"
2025年4月21日 17:53
Your post is so Amazing and Helpful. I would definitely recommend it to others. Thank you for sharing this Important Topic.This post is amazing and much interesting to me as you cover very important topic in this post. I would recommend this beneficial information with others.This post is amazing and much interesting to me as you cover very important topic in this post. I would recommend this beneficial information with others.thank you
2025年4月21日 18:51
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.
2025年4月21日 20:16
This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks
2025年4月21日 20:17
Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again.
2025年4月21日 20:18
"This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.
"
2025年4月21日 20:18
"What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! Thanks for sharing this information. I really like your blog post very much.
"
2025年4月21日 20:19
Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again.